Java puzzle – Why try-catch-finally blocks require braces?

We all know how to write try-catch-finally blocks in Java. Even we all have been writing if-else statements also. Lets understand why try-catch-finally blocks require braces?

1. Braces in if-else block – works fine

Lets write one if-else block.

boolean condition = false;  
     
if(condition)            
	System.out.println("OK");        
else            
	System.out.println("Not so OK");

Above code will be compiled without any error reported by compiler. Compiler understands that else block is related to if block written above.

Lets add another else block.

if(conditionOne)
if(conditionTwo)
 	System.out.println("OK");
else
 	System.out.println("Not so OK");
else
 	System.out.println("Not so OK");
 

Now this code becomes a little scary. If we remove the indentation, it becomes more scary and hard to understand. Yet, compiler passes it with flying colors. Because it is still able to determine the related if blocks for any given else block, by pairing first encountered if block, which is still not paired (going from down to upwards).

2. Braces in try-catch-finally block

Now, lets write try-catch block without pairing braces.

try
	System.out.println("try one");
catch(NullPointerException npe)
	//handle npe

Strange, compiler is not able to make sense of above simple construct. Compiler wants us to add braces in try and catch blocks. Why??

To understand this, lets expand try-catch block with some more statements.

try
	System.out.println("try one");
try
	System.out.println("try two");
catch(NullPointerException npe)
	System.out.println("catch one");
catch(Exception e)
	System.out.println("catch two");
finally
	System.out.println("finally");

How you will resolve above statements. We can resolve above statements in many ways. For example,

Option 1.

try
{
	System.out.println("try one");
	try
	{
		System.out.println("try two");
	} catch (NullPointerException npe) {
		System.out.println("catch one");
	}
}
catch (Exception e)
{
	System.out.println("catch two");
}
finally
{
	System.out.println("finally");
}

Option 2.

try
{
	System.out.println("try one");
	try
	{
		System.out.println("try two");
	} catch (NullPointerException npe) {
		System.out.println("catch one");
	}
	catch (Exception e)
	{
		System.out.println("catch two");
	}
}
finally
{
	System.out.println("finally");
}

Clearly, above both code constructs are correct and so compiler get confused, how to interpret try-catch blocks.

3. Reason

Main reason seems to be multiple catch blocks are allowed for a single try block, where as is not allowed in case of if-else statements.

This is why java designers enforced the usage of braces in try-catch-finally blocks. So now we know why try-catch-finally blocks require braces.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.