The difference between throw and throws and the order in which try, catch and finally are executed with return

1、 There are three forms of throwing exceptions. One is throw, the other is throws, and the other is that the system throws exceptions automatically. Here are the similarities and differences between them.

(1) . the system automatically throws exceptions

1. When there are some logic errors, syntax errors or type conversion errors in the program statement, the system will automatically throw an exception:

The system will automatically throw an arithmeticexception exception.

two

The system will automatically throw numberformatexception exception.

(2)、throw

Throw is an exception thrown by a statement, which is generally inside the code block. When a certain logic error occurs in the program, the programmer actively throws a certain type of exception

When running, the system will throw an exception:

Exception in thread "main" java. lang.NumberFormatException

(3)、throws

Throws is a declaration that a method may throw an exception. (used when declaring a method to indicate that the method may throw an exception)

When a method may throw an exception, it is used to declare the exception that may be thrown, and then hand it to the upper layer method program that calls it for processing

Operation results:

Non data types cannot be cast.

(4) , throw and throws comparison: throws appears in the method function header, while throw appears in the function body. Throws indicates a possibility of exceptions, and these exceptions do not necessarily occur; throw means that an exception is thrown, and execution of throw must throw an exception object. Both are negative exception handling methods (the negativity here does not mean that this method is not good). It just throws or may throw exceptions, but the function will not handle the exceptions. The real exceptions are handled by the upper layer call of the function.

(5) Programming habits

When writing a program, try {...} is usually used for the parts where exceptions may occur catch{...} To capture it and process it; Try {...} catch{...} After catching the exception, be sure to catch {...} Even the simplest output statement or stack input e.printstacktrace(); If you want to catch exceptions in the IO input / output stream, be sure to try {...} catch{...} Finally {...} Turn off the input / output stream; If an exception is thrown with throw in the function body, it is best to add the throw exception declaration to the function name, and then hand it over to the upper function calling it for processing.

2、 Try, catch, finally the order of execution with return

Conclusion:

1. The code in the finally block will be executed regardless of any exception;

2. When there is a return in try and catch, finally will still execute;

3. Finally is executed after the expression operation after return (at this time, the calculated value is not returned, but the value to be returned is saved first. Regardless of the code in finally, the returned value will not change, but still the previously saved value). Therefore, the return value of the function is determined before finally execution;

4. Finally, it is better not to include return, otherwise the program will exit in advance, and the return value is not the return value saved in try or catch.

give an example:

Case 1:

Obviously, the program is executed in sequence.

Case 2:

The program executes the code before the return in the try block (including the expression operation in the return statement); then executes the finally block, and finally executes the return in the try; the statement after the finally block returns, because the program has returned in the try, it will not be executed. Case 3:

The program executes try first. If an exception occurs, execute the catch block. If there is an exception: execute the code before return (including the expression operation in the return statement) in the catch, then execute all the code in the finally statement, and finally execute return in the catch block. After finally, the code at 4 will not be executed. No exception: execute try and then finally and then return. Case 4:

The program executes the code before the return in the try block (including the expression operation in the return statement); then executes the finally block. Because there is a return in the finally block, it exits early. Case 5:

The program executes the code before the return in the catch block (including the expression operation in the return statement); then executes the finally block. Because there is a return in the finally block, it exits early. Case 6:

The program executes the code before the return in the try block (including the expression operation in the return statement); there is an exception: before the return in the catch block (including the expression operation in the return statement) code; then execute the finally block, and exit early because there is a return in the finally block. No exception: then execute the finally block, and exit early because there is a return in the finally block. Final conclusion:

Before any return statement in try or catch is executed, the final statement will be executed first. If finally exists, if there is a return statement in finally, the program will return. Therefore, the return in finally will be returned. The compiler implements the return in finally as a warning.

下面是个测试程序

The result is 2.

analysis:

In the try statement, when executing the return statement, the results to be returned are ready. At this time, the program goes to finally execution. Before switching, the result to be returned is stored in a local variable different from X in try. After finally, the returned result is taken out. Therefore, even if the variable x is changed in finally, the returned result will not be affected. It should use the stack to save the return value.

Reference blog:

https://blog.csdn.net/hjfcgt123/article/details/53349275

https://blog.csdn.net/kavensu/article/details/8067850

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>