Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between try-finally and try-catch

What's the difference between

try {     fooBar(); } finally {     barFoo(); } 

and

try {   fooBar(); } catch(Throwable throwable) {     barFoo(throwable); // Does something with throwable, logs it, or handles it. } 

I like the second version better because it gives me access to the Throwable. Is there any logical difference or a preferred convention between the two variations?

Also, is there a way to access the exception from the finally clause?

like image 704
Vijay Kotari Avatar asked May 18 '10 06:05

Vijay Kotari


People also ask

What is the difference between try Except and try finally?

The try block lets you test a block of code for errors. The except block lets you handle the error. The else block lets you execute code when there is no error. The finally block lets you execute code, regardless of the result of the try- and except blocks.

What is the difference between try and catch?

The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

What is the difference between the try catch and try catch finally programming constructors?

The catch block is only executed if an exception is thrown in the try block. The finally block is executed always after the try(-catch) block, if an exception is thrown or not.

What is difference between try catch and throws?

throw keyword will allow you to throw an exception and it is used to transfer control from try block to catch block. throws keyword is used for exception handling without try & catch block. It specifies the exceptions that a method can throw to the caller and does not handle itself.


2 Answers

These are two different things:

  • The catch block is only executed if an exception is thrown in the try block.
  • The finally block is executed always after the try(-catch) block, if an exception is thrown or not.

In your example you haven't shown the third possible construct:

try {     // try to execute this statements... } catch( SpecificException e ) {     // if a specific exception was thrown, handle it here } // ... more catches for specific exceptions can come here catch( Exception e ) {     // if a more general exception was thrown, handle it here } finally {     // here you can clean things up afterwards } 

And, like @codeca says in his comment, there is no way to access the exception inside the finally block, because the finally block is executed even if there is no exception.

Of course you could declare a variable that holds the exception outside of your block and assign a value inside the catch block. Afterwards you can access this variable inside your finally block.

Throwable throwable = null; try {     // do some stuff } catch( Throwable e ) {     throwable = e; } finally {     if( throwable != null ) {         // handle it     } } 
like image 188
tangens Avatar answered Oct 17 '22 09:10

tangens


These are not variations, they're fundamentally different things. finally is executed always, catch only when an exception occurs.

like image 28
Michiel Buddingh Avatar answered Oct 17 '22 08:10

Michiel Buddingh