Java Try Catch Finally blocks without Catch

JavaTry CatchFinallyTry Catch-FinallyTry Finally

Java Problem Overview


I'm reviewing some new code. The program has a try and a finally block only. Since the catch block is excluded, how does the try block work if it encounters an exception or anything throwable? Does it just go directly to the finally block?

Java Solutions


Solution 1 - Java

If any of the code in the try block can throw a checked exception, it has to appear in the throws clause of the method signature. If an unchecked exception is thrown, it's bubbled out of the method.

The finally block is always executed, whether an exception is thrown or not.

Solution 2 - Java

A small note on try/finally: The finally will always execute unless

  • System.exit() is called.
  • The JVM crashes.
  • The try{} block never ends (e.g. endless loop).

Solution 3 - Java

The Java Language Specification(1) describes how try-catch-finally is executed. Having no catch is equivalent to not having a catch able to catch the given Throwable.

> * If execution of the try block completes abruptly because of a throw of a value V, then there is a choice:
> * If the run-time type of V is assignable to the parameter of any catch clause of the try statement, then …
> …
> * If the run-time type of V is not assignable to the parameter of any catch clause of the try statement, then the finally block is executed. Then there is a choice:
> * If the finally block completes normally, then the try statement completes abruptly because of a throw of the value V.
> * If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and the throw of value V is discarded and forgotten).


(1) Execution of try-catch-finally

Solution 4 - Java

The inner finally is executed prior to throwing the exception to the outer block.

public class TryCatchFinally {

  public static void main(String[] args) throws Exception {

	try{
		System.out.println('A');
		try{
			System.out.println('B');
			throw new Exception("threw exception in B");
		}
		finally
		{
			System.out.println('X');
		}
		//any code here in the first try block 
		//is unreachable if an exception occurs in the second try block
	}
	catch(Exception e)
	{
		System.out.println('Y');
	}
	finally
	{
		System.out.println('Z');
	}
  }
}

Results in

A
B
X
Y
Z

Solution 5 - Java

The finally block is always run after the try block ends, whether try ends normally or abnormally due to an exception, er, throwable.

If an exception is thrown by any of the code within the try block, then the current method simply re-throws (or continues to throw) the same exception (after running the finally block).

If the finally block throws an exception / error / throwable, and there is already a pending throwable, it gets ugly. Quite frankly, I forget exactly what happens (so much for my certification years ago). I think both throwables get linked together, but there is some special voodoo you have to do (i.e. - a method call I would have to look up) to get the original problem before the "finally" barfed, er, threw up.

Incidentally, try/finally is a pretty common thing to do for resource management, since java has no destructors.

E.g. -

r = new LeakyThing();
try { useResource( r); }
finally { r.release(); }  // close, destroy, etc

"Finally", one more tip: if you do bother to put in a catch, either catch specific (expected) throwable subclasses, or just catch "Throwable", not "Exception", for a general catch-all error trap. Too many problems, such as reflection goofs, throw "Errors", rather than "Exceptions", and those will slip right by any "catch all" coded as:

catch ( Exception e) ...  // doesn't really catch *all*, eh?

do this instead:

catch ( Throwable t) ...

Solution 6 - Java

Java versions before version 7 allow for these three combinations of try-catch-finally...

try - catch
try - catch - finally
try - finally

finally block will be always executed no matter of what's going on in the try or/and catch block. so if there is no catch block, the exception won't be handled here.

However, you will still need an exception handler somewhere in your code - unless you want your application to crash completely of course. It depends on the architecture of your application exactly where that handler is.

> - Java try block must be followed by either catch or finally block. > - For each try block there can be zero or more catch blocks, but only one finally block. > - The finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).

Solution 7 - Java

> how does the try block work if it > encounters an exception or anything > throwable

The exception is thrown out of the block, just as in any other case where it's not caught.

The finally block is executed regardless of how the try block is exited -- regardless whether there are any catches at all, regardless of whether there is a matching catch.

The catch blocks and the finally are orthogonal parts of the try block. You can have either or both. With Java 7, you'll be able to have neither!

Solution 8 - Java

Don't you try it with that program? It'll goto finally block and executing the finally block, but, the exception won't be handled. But, that exception can be overruled in the finally block!

Solution 9 - Java

The finally block is executed after the try block completes. If something is thrown inside the try block when it leaves the finally block is executed.

Solution 10 - Java

Inside try block we write codes that can throw an exception. The catch block is where we handle the exception. The finally block is always executed no matter whether exception occurs or not.

Now if we have try-finally block instead of try-catch-finally block then the exception will not be handled and after the try block instead of control going to catch block it will go to finally block. We can use try-finally block when we want to do nothing with the exception.

Solution 11 - Java

Regardless of exception thrown or not in try block - finally block will be executed. Exception would not be caught.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNullPointer0x00View Question on Stackoverflow
Solution 1 - JavaduffymoView Answer on Stackoverflow
Solution 2 - JavaPeter LawreyView Answer on Stackoverflow
Solution 3 - Javauser85421View Answer on Stackoverflow
Solution 4 - JavaskipchaserView Answer on Stackoverflow
Solution 5 - JavaRoboprogView Answer on Stackoverflow
Solution 6 - JavaroottravellerView Answer on Stackoverflow
Solution 7 - JavaAndy ThomasView Answer on Stackoverflow
Solution 8 - JavaAbimaran KugathasanView Answer on Stackoverflow
Solution 9 - JavamP.View Answer on Stackoverflow
Solution 10 - JavaAnimesh JaiswalView Answer on Stackoverflow
Solution 11 - JavakrekerView Answer on Stackoverflow