What is the difference between final and final?

What's the difference between

try { // action A } catch(Exception e) { // action B } finally { // action C } 

and

 try { // action A } catch(Exception e) { // action B } // action C 

I read that you can go back inside the catch and still execute the finally block. Are there any other differences?

+6
source share
6 answers

Things that happen in the finally block are guaranteed to happen regardless of what happens in the try-catch block. If an exception occurs that is not encapsulated with Exception (for example, extends Throwable , for example, various Error s), then it still runs the finally block.

One thing to be aware of: if a RuntimeException or other Exception screens are RuntimeException from it in the finally block, the rest of the finally block will not be executed. Also, as Lord Torgamus pointed out, it depends on the work of the JVM. In addition, and it is probably obvious that this also depends on the thread not being stopped.

+8
source

Most existing answers contain fragments of the correct answer, but none of them are accurate enough.

The finally block will always be reached after try and potentially catch blocks, unless the JVM closes in advance. However, if a bit of code inside the finally block disables the JVM or throws an exception, the end of the block may not be reached.

For a Sun Certified Programmer for Java 6 Tutorial:

  • The only exception to the finally -will-always-be-called rule is that finally will not be called if the JVM shuts down.

  • Just because calling finally does not mean that it will be completed.

The last word, as always, is a specification of the Java language. The finally behavior is fully explained in ยง14.20.2 Executing try-catch-finally .

As an additional note: you are correct that return in try will not stop the execution of finally . In fact, finally is introduced immediately after return is encountered, before .

+5
source

Better look at this example, resources are always closed, even if I go back or spread Excetchion up.

 try { //action A return 0; } catch (Exception e){ //action C throw new Excpetion("Probleme here",e) } finnaly { //action C resources.close(); } 

If the actions A and be are as primitive as int a = 0, then there is no difference, but in more complex situations like this, the finnaly block uses it

+2
source

from Sun Tutorials

Note. If the JVM exits during the attempt or the catch code, then the finally block will not be executed. Similarly, if the thread attempting to catch or trick the code is interrupted or killed, the finally block will not be executed, although the application as a whole continues.

I do not know other ways that the finally block would not execute ...

+1
source

The code inside the finally block is guaranteed to execute if the JVM continues.

This means that even if what is in action B throws another new exception or does not raise an exception in action A or returns, the code in action C is executed.

0
source

An exception block is used to handle exceptions. If there is any case / code that will throw an exception, for example, if we divide the number by Zero (0), than it will throw an exception, and the process will be completed. In this case, if we put our code in a try block, and the exception is a catch catch-block, and the process will not be completed. And finally, block the guarantee of the execution of the code written there. Therefore, if we need to stop / close the process, even its execution will be successful or not (for example, in the case of network programming, we must finally disconnect the connection so that other devices can use this connection), we use the finally block.

0
source

Source: https://habr.com/ru/post/886971/


All Articles