When will it finally be fulfilled?

I have this Java code with a nested try:

try { try { [ ... ] { catch (Exception ex) { showLogMessage(ex); return; } while (condition == true) { try { [ ... ] { catch (Exception ex) { showLogMessage(ex); continue; } [ ... ] } } catch (NumberFormatException e) { showLogMessage(e); } finally { doSomeThingVeryImportant(); } 

I want to know if finally is executed when I get an exception. I ask this because catch blocks have return or continue expressions.

When is doSomeThingVeryImportant() executed? When do I get an Exception , when do I get a NumberFormatException ?

All I need is for the finally block to be executed after any catch block is executed.

+4
source share
10 answers

The finally block always executes when the try block exits ( click ).

+1
source

The finally block, if used, is placed after the try block and the catch blocks that follow it. The finally block contains code that will be run regardless of whether an exception is thrown in the try block. The general syntax is as follows:

 public void someMethod{ Try { // some code } Catch(Exception x) { // some code } Catch(ExceptionClass y) { // some code } Finally{ //this code will be executed whether or not an exception //is thrown or caught } } 

There are 4 possible scenarios here:

  • The try block runs to the end, and no exception is thrown. In this case, the finally block will be executed after the try block.

  • An exception was thrown in the try block, which then fell into one of the catch blocks. In this case, the finally block will be executed immediately after the catch block is executed.

  • An exception is thrown in the try block and there is no corresponding catch block in the method that can throw an exception. In this case, the method call ends and the exception object is passed to the method - as in the method in which try-catch-finally blocks. But, before the method ends, finally the block is executed.

  • Before the try block completes, it will return to wherever the method was called. But, before it returns to the calling method, the code in the finally block is still executed. So, remember that the code in the finally block will execute even if there is a return somewhere in the try block.

Update: Finally, ALWAYS is executed, regardless of what happens in the try or catch block (fail, return, exception, finish, etc.).

+18
source
 The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs 

Taken from here: http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html

Further, the page explains that the finally may not block is executed if the JVM exists when try or catch code is executed, or if the thread executing try / catch is interrupted or killed.

Therefore, if you cannot kill the JVM, or try / catch is executed in a thread, the finally block will always be executed

+2
source

Yes, and finally it is always fulfilled. Exclusion and Exclusion of NO.

This is a way to make sure that some part of the code always gets.

It is used, for example, to delete objects, close open connections to the server and such materials.

Check out this link from oracle:

http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html

+2
source

Yes, finally blocks are always executed.

Source: http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html

+2
source

Yes; or at least as close to "always." (So, even if you have return or another throw .)

If your process is killed or your program gets stuck in a dead end or an endless loop, or your device is hit by a meteor, then the program flow will not reach the finally block.

+2
source

Say your code block looks something like this:

  try { try { System.out.println("first try block"); } catch (Exception ex) { System.out.println("first cathc block"); return; } while (true) { try { System.out.println("second try block..."); } catch (Exception ex) { System.out.println("second catch block"); continue; } System.out.println("end of loop"); break; } } catch (NumberFormatException e) { System.out.println("last catch block"); } finally { System.out.println("finally block"); } 

If you run this, you will get the following result:

 first try block second try block... end of loop finally block 

So, finally block is executed anyway.

+2
source

Yes, finally, it is always executed, and this is because it is possible to execute a command to close / process resources in case of exceptions or without exception.

http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html


Refresh to answer the question in the comment on System.exit()

The finally block will not be executed in the case of System.exit() if it does not work with some SecurityException .


Update after changing the question. No matter what the exception is, the finally block will always execute.

If it is an Exception , then your catch block will not be executed, since it catches only NumberFormatException

0
source

Yes, finally, blocks are always executed. But the terms and conditions apply.

Until you name System.exit ();

0
source

No, it will not be finally executed always. There are two situations in which execution will finally fail.

Note : If the JVM terminates while try or catch code is running, the finally block may not be executed. Similarly, if a thread executing try or catch code is interrupted or killed, the finally block may not be executed, even if the application as a whole continues.

java link

According to your question, if these two things do not exist, something inside yours will finally be fulfilled, it is necessary.

0
source

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


All Articles