In this article, we’ll explore all the possible combinations of try-catch-finally which may happen whenever an exception is raised and how the control flow occurs in each of the given cases.
- Control flow in try-catch clause OR try-catch-finally clause
- Case 1: Exception occurs in try block and handled in catch block
- Case 2: Exception occurs in try-block is not handled in catch block
- Case 3: Exception doesn’t occur in try-block
- try-finally clause
- Case 1: Exception occurs in try block
- Case 2: Exception doesn’t occur in try-block
Control flow in try-catch OR try-catch-finally
1. Exception occurs in try block and handled in catch block: If a statement in try block raised an exception, then the rest of the try block doesn’t execute and control passes to the corresponding catch block. After executing the catch block, the control will be transferred to finally block(if present) and then the rest program will be executed.
- Control flow in try-catch:
Java
class GFG
{
public static void main (String[] args)
{
int [] arr = new int [ 4 ];
try
{
int i = arr[ 4 ];
System.out.println( "Inside try block" );
}
catch (ArrayIndexOutOfBoundsException ex)
{
System.out.println( "Exception caught in Catch block" );
}
System.out.println( "Outside try-catch clause" );
}
}
|
OutputException caught in Catch block
Outside try-catch clause
- Control flow in try-catch-finally clause :
Java
class GFG
{
public static void main (String[] args)
{
int [] arr = new int [ 4 ];
try
{
int i = arr[ 4 ];
System.out.println( "Inside try block" );
}
catch (ArrayIndexOutOfBoundsException ex)
{
System.out.println( "Exception caught in catch block" );
}
finally
{
System.out.println( "finally block executed" );
}
System.out.println( "Outside try-catch-finally clause" );
}
}
|
OutputException caught in catch block
finally block executed
Outside try-catch-finally clause
2. Exception occurred in try-block is not handled in catch block: In this case, the default handling mechanism is followed. If finally block is present, it will be executed followed by the default handling mechanism.
Java
class GFG
{
public static void main (String[] args)
{
int [] arr = new int [ 4 ];
try
{
int i = arr[ 4 ];
System.out.println( "Inside try block" );
}
catch (NullPointerException ex)
{
System.out.println( "Exception has been caught" );
}
System.out.println( "Outside try-catch clause" );
}
}
|
Run Time Error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at GFG.main(GFG.java:12)
- try-catch-finally clause :
Java
class GFG
{
public static void main (String[] args)
{
int [] arr = new int [ 4 ];
try
{
int i = arr[ 4 ];
System.out.println( "Inside try block" );
}
catch (NullPointerException ex)
{
System.out.println( "Exception has been caught" );
}
finally
{
System.out.println( "finally block executed" );
}
System.out.println( "Outside try-catch-finally clause" );
}
}
|
Output :
finally block executed
Run Time error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at GFG.main(GFG.java:12)
3. Exception doesn’t occur in try-block: In this case catch block never runs as they are only meant to be run when an exception occurs. finally block(if present) will be executed followed by rest of the program.
Java
class GFG
{
public static void main (String[] args)
{
try
{
String str = "123" ;
int num = Integer.parseInt(str);
System.out.println( "Inside try block" );
}
catch (NumberFormatException ex)
{
System.out.println( "catch block executed..." );
}
System.out.println( "Outside try-catch clause" );
}
}
|
OutputInside try block
Outside try-catch clause
Java
class GFG
{
public static void main (String[] args)
{
try
{
String str = "123" ;
int num = Integer.parseInt(str);
System.out.println( "try block fully executed" );
}
catch (NumberFormatException ex)
{
System.out.println( "catch block executed..." );
}
finally
{
System.out.println( "finally block executed" );
}
System.out.println( "Outside try-catch-finally clause" );
}
}
|
Outputtry block fully executed
finally block executed
Outside try-catch-finally clause
Control flow in try-finally
In this case, no matter whether an exception occurs in try-block or not finally will always be executed. But control flow will depend on whether an exception has occurred in the try block or not.
1. Exception raised: If an exception has occurred in the try block then the control flow will be finally block followed by the default exception handling mechanism.
Java
class GFG
{
public static void main (String[] args)
{
int [] arr = new int [ 4 ];
try
{
int i = arr[ 4 ];
System.out.println( "Inside try block" );
}
finally
{
System.out.println( "finally block executed" );
}
System.out.println( "Outside try-finally clause" );
}
}
|
Output :
finally block executed
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at GFG.main(GFG.java:11)
2. Exception not raised: If an exception does not occur in the try block then the control flow will be finally block followed by the rest of the program
Java
class GFG
{
public static void main (String[] args)
{
try
{
String str = "123" ;
int num = Integer.parseInt(str);
System.out.println( "Inside try block" );
}
finally
{
System.out.println( "finally block executed" );
}
System.out.println( "Outside try-finally clause" );
}
}
|
OutputInside try block
finally block executed
Outside try-finally clause
Related Articles:
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.