The difference between ending and recording after the catch

What is the difference between finally and write after catch?

For example:

public boolean example() { try { // Code } catch (RuntimeException e) { // Code } finally { return true; } } public boolean example() { try { // Code } catch (RuntimeException e) { // Code } return true; } 
+8
java exception try-catch-finally try-catch
source share
10 answers

First of all, the only idiom that can even be considered a candidate is this:

 try { // stuff } catch (Throwable t) { /* handle */ } // finally stuff 

Pay attention to the captured type. Only if you catch any possible exception, including such dark monsters as ThreadDeath and VirtualMachineError , can you hope to unconditionally reach the code below try-catch.

But this is only where it begins. What if the processing code itself throws an exception? So you need at least

 try { /* main stuff */ } catch (Throwable t) { try { /* handle */ } catch (Throwable t) { // no code allowed here! Otherwise we descend into infinite recursion } } /* finally stuff */ 

Now you can begin to understand the benefits of finally , but that’s not all. Consider a fairly typical scenario:

 try { /* main stuff, may throw an exception. I want the exception to break what I'm doing here and propagate to the caller */ return true; } finally { /* clean up, even if about to propagate the exception */ } 

How do you rewrite this? Without duplication of code is impossible.

+4
source share

There is no difference in the code you provided. But finally, it is used to run some piece of code after the try block, regardless of whether there is an exception or not.

The interesting point here is that we should avoid returning from the finally block, because it can create confusion in the script when we return something from the try block. Consider this piece of code:

 try { return true; } finally { return false; } 

Now this code will return false no matter what happens when you try. In many ways, this behavior is exactly consistent with the colloquial concept of what ultimately means - "regardless of what happens in advance in the try block, always run this code." Therefore, if you return true from the finally block, the overall effect should always be to return true, no?

In general, this is rarely a good idiom, and you should use finally blocks generously to clean / close resources, but rarely if you ever return a value from them.

+3
source share

In your case there is no difference. but

The runtime system always executes instructions in the final block no matter what happens in the try block.

So this is the perfect place to do the cleaning.

Note that if you are using JDK 7+, then most uses of the finally block can be eliminated simply by using the try-with-resources statement .

+2
source share

We use the try block, catch block, and finally block the Exception processing in our program. The program may include checking or removing an exception. therefore, this block is used to handle this Exception. In the try block, we mention or write down the code that may be the cause of the exception, and if we want our code to be executed if an exception occurs. Then we write this code to the catch block. finally, it is a very special block that gives us a special feature, which, if our catch block does not start, before the program ends, to finally execute the block code. This is mainly used to save our data during an unwanted program stop. if we use a try block, then after the try block there should be a catch block, but, finally, optional optional.

+1
source share

The first thing to understand in this question is "why do we use try {} catch {} block" Ok.

The answer is that there is the possibility of our exception exception code.

This kind of code that we put in try {...} block and catch {...} contain code to catch the exception generated by the code in try {} block.

Finally, the {...} block contains the code executed immediately after try {} catch {} when try {} blocks the exception.

For example, when you visit a website, but make a server-side problem, it cannot even display a message on the page, for example, "error or error 404", this code is written in the finally block.

0
source share

this simple catch is intended only for catch eXception and handles it, but is finally executed if an exception or not, for example, for closed connections.

0
source share

Finally, the block is useful for cleaning your code, for example, closing open threads.

eg:

  File f = new File("\file.txt"); FileStream fs; try{ fs = new FileStream(f); } catch (IOException ioe){ ioe.printStackTrace(); } finally { if (fs != null) { fs.close() // close the stream } } 
0
source share

As soon as your code returns true/false , so this will not affect much. But think about any other code / logic where some kind of mandatory / cleansing code is written to execute.

In addition, it will be difficult to catch all exceptions through our code or, in other words, we must catch certain exceptions that we can handle. At this point, finally will be a real savior, since it will always be executed (except for System.exit() or in some recursive case).

In addition, to ensure consistency in our code, you must always use the finally block to execute any clearing code.

You can also refer to the messages below for further clarification:

  • Using finally block vs to write code after try / catch block
  • Explanation of the attempt-final return
0
source share

The first case:

If an exception occurs in the try block, then the catch block will be executed, and when servicing the same catch block, if an exception occurs again, then the finally block will be executed.

Second case:

If an exception occurs in the try block, then the catch block will be executed, and if there is another exception in one catch block, then "return true"; will be executed.

0
source share

finally () is used whenever we want some line of code to be executed, even if an exception occurs. But if you write a line of code after catch, it will not be executed if an exception occurs.

-2
source share

All Articles