In this post we will see the exception handling using the try catch finally in java.
1. Use of Try – Catch Block
A exception in java can be handled using a Try and Catch block.
Lets see a simple example where we have some statements to execute.But one of statement will cause an exception. Now we have to handle this exception and tell the user about the exception.In that case we will need to use the try-catch block. Here the catch block catches the exception and executes the statement inside the catch block and then passes the control back to the next statement if available, else ends the program gracefully.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.kscodes.sampleproject; public class SimpleExceptionHandling { public static void main(String[] args) { try { System.out.println("We are in the Try block and performing i/j "); int i = 10; int j = 0; int k = i / j; System.out.println("Result is k -" + k); } catch (Exception e) { System.out.println("An exception occurred when performing i/j"); } } } |
Output
1 2 |
We are in the Try block and performing i/j An exception occurred when performing i/j |
2. Use of Try – Catch – Finally Block
There are some scenarios where you need to execute some statements regardless of an exception or even if no exception occurs.
For example : You have opened a Database connection and you need to close that connection even if an exception occurs.
Or You are writing/reading a File and you need to close the stream regardless of an exception.
In that scenario we use the finally block.
Some points to remember about finally block ::
-
For a code having try-catch-finally the control flow is
- If no exception occurs in try block, then all the statements in try block are executed and then control moves to finally block.
- If an exception occurs in try block, then control immediately moves to catch block(remaining statements in try block are not executed), all the statements in catch block are executed and then control moves to finally block.
-
We can write a try block without a catch block. But in that case we need to have a finally block
In that case the control flow will be
- If no exception occurs in try block, then all the statements in try block are executed and then control moves to finally block.
- If an exception occurs in try block, then control immediately moves to finally block, all the remaining statements in try block are not executed.
Lets see an example of FileStream and its closing where we use Finally block
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.kscodes.sampleproject; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class SimpleExceptionHandling { public static void main(String[] args) { FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream("C:\\SomeTempFile"); } catch (FileNotFoundException e) { System.out.println("An exception occured :: " + e.getMessage()); System.out.println("Need to close FileStream"); } finally { System.out.println("In Finally Block - Thank God we are here :) "); try { if (fileInputStream != null) { fileInputStream.close(); } } catch (IOException e) { } } } } |
Output
1 2 3 |
An exception occured :: C:\SomeTempFile (The system cannot find the file specified) Need to close FileStream In Finally Block - Thank God we are here :) |
What happens if we have return statement in Try or Catch or Finally?
You can find more details on this question here