In this tutorial, I’ll explain about the flow control of try catch and finally blocks in accordance to the exception.
What is try block in Java?
It is used to specify a block where we should place exception code. The try block must be followed by either catch or finally. It means, we can’t use try block alone.
If an exception occurs at the particular statement of try block, the rest of the block code will not execute. So, it is recommended not to keeping the code in try block that will not throw an exception.
What is catch block in Java?
It is used to handle the exception by declaring the type of exception within the parameter. It must be preceded by try block which means we can’t use catch block alone. It can be followed by finally block later.
You can use multiple catch block with a single try block.
What is finally block in Java?
It is used to execute important statements such as establishing connection, stream, etc. It is always executed whether exception is handled or not.
Syntax of try-catch block
try{
//throw an exception
}catch{ //handle exception }
Syntax of try-catch-finally block
try{
//throw an exception
}catch { //handle exception }
finally { //important code which will always be executed }
1.Exception occurs in try block and handled in catch block
If a statement in try block throws an exception, then rest of the code of try block is not executed. The control transfers to corresponding catch block. After the execution of code in catch block, the control moves to the rest of the program code to be executed.
public class TryCatchDemo {
public static void main(String[] args) {
{
try {
String a = null; // null value
System.out.println(a.length());
// This will never execute as try block has exception just above this statement
System.out.println("Inside try block");
// Exception will be handled in catch block and will execute the statements in
// catch block
} catch (NullPointerException e) {
System.out.println("NullPointer Exception - Exception caught in Catch block");
}
// Rest of the program will be executed
System.out.println("Outside try-catch block");
}
}
}
Output
NullPointer Exception - Exception caught in Catch block
Outside try-catch block
2. Exception occurs in try block and handled in catch block and finally block is present
If a statement in try block throws an exception, then rest of the code of try block is not executed. The control transfers to corresponding catch block. After the execution of code in catch block, the control moves to finally block (if present) and then the rest of the program is executed.
public class TryCatchFinallyDemo {
public static void main(String[] args) {
{
try {
String a = null; // null value
System.out.println(a.length());
// This will never execute as try block has exception just above this statement
System.out.println("Inside try block");
// Exception will be handled in catch block and will execute the statements in
// catch block
} catch (NullPointerException e) {
System.out.println("NullPointer Exception - Exception caught in Catch block");
// Statement present in finally block will be executed irrespective whether
// exception is handled or not
} finally {
System.out.println("finally block executed");
}
// Rest of the program will be executed
System.out.println("Outside try-catch block");
}
}
}
Output
NullPointer Exception - Exception caught in Catch block
finally block executed
Outside try-catch block
3. Exception occurred in try-block is not handled in catch block without finally block
If a statement in try block throws an exception, then rest of the code of try block is not executed.
public class TryNoCatchDemo {
public static void main(String[] args) {
{
try {
String a = null; // null value
System.out.println(a.length());
// This will never execute as try block has exception just above this statement
System.out.println("Inside try block");
// Incorrect Exception
} catch (IndexOutOfBoundException e) {
System.out.println("IndexOutOfBound Exception - Exception caught in Catch block");
}
// Rest of the program will be executed
System.out.println("Outside try-catch block");
}
}
}
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
IndexOutOfBoundException cannot be resolved to a type
at JavaDemo.Exception.TryNoCatchDemo.main(TryNoCatchDemo.java:15)
4. Exception occurred in try-block is not handled in catch block with finally block
public class TryNoCatchFinallyDemo {
public static void main(String[] args) {
{
try {
String a = null; // null value
System.out.println(a.length());
// This will never execute as try block has exception just above this statement
System.out.println("Inside try block");
// Incorrect Exception
} catch (IndexOutOfBoundException e) {
System.out.println("IndexOutOfBound Exception - Exception caught in Catch block");
}
finally {
System.out.println("finally block executed");
}
// Rest of the program will be executed
System.out.println("Outside try-catch block");
}
}
}
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
IndexOutOfBoundException cannot be resolved to a type
at JavaDemo.Exception.TryNoCatchDemo.main(TryNoCatchDemo.java:15)
5. Exception doesn’t occur in try-block
If a statement in try block does not throw an exception, then catch block will be never executed and then rest of the program will be executed.
public class NoTryCatchDemo {
public static void main(String[] args) {
try {
String str = "123";
int num = Integer.parseInt(str);
// this statement will execute
// as no any exception is raised by above statement
System.out.println("Inside try block");
}
catch (NumberFormatException ex) {
System.out.println("catch block executed.");
}
System.out.println("Outside try-catch clause");
}
}
Output
Inside try block
Outside try-catch clause
6. Exception doesn’t occur in try-block with finally block
If a statement in try block does not throw an exception, then catch block will never be executed. But the finally block will be executed and then rest of the program will be executed.
public class NoTryCatchFinallyDemo {
public static void main(String[] args) {
try {
String str = "123";
int num = Integer.parseInt(str);
// this statement will execute
// as no any exception is raised by above statement
System.out.println("Inside try block");
}
catch (NumberFormatException ex) {
System.out.println("catch block executed");
} finally {
System.out.println("finally block executed");
}
System.out.println("Outside try-catch clause");
}
}
Output
Inside try block
finally block executed
Outside try-catch clause