Flow control in try catch finally in Java programming.

Java 8Object Oriented ProgrammingProgramming

An exception is an issue (run time error) that occurred during the execution of a program. Some exceptions are prompted at compile time these are known at compile-time exceptions or checked exceptions.

If an exception occurs the program terminates abruptly at the line that caused exception, leaving the remaining part of the program unexecuted. To prevent this, you need to handle exceptions.

Try, catch, finally blocks

To handle exceptions Java provides a try-catch block mechanism.

The try block − A try block is placed around the code that might generate an exception. Code within a try/catch block is referred to as a protected code.

Syntax

try {
   // Protected code
}
 catch (ExceptionName e1) {
   // Catch block
}

The catch block − A catch block involves declaring the type of exception you are trying to catch. If an exception occurs in the try block, the catch block (or blocks) that follows the try is checked. If the type of exception that occurred is listed in a catch block, the exception is passed to the catch block much as an argument is passed into a method parameter.

The finally block − The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of the occurrence of an Exception.

Flow control in exceptions

In exception handling, you can have try-catch, try-finally and, try-catch-finally blocks. In these, you might have the following scenarios −

If Exception doesn’t occur − If no exception raised in the try block the code in the catch block (in try-catch or try-catch-finally) doesn’t get executed. Anyhow the finally block gets executed always (try-finally or, try-catch-finally)

If Exception occurs in try-catch − When an exception raised inside a try block, instead of terminating the program JVM stores the exception details in the exception stack and proceeds to the catch block.

If the type of exception that occurred is listed/handled in a catch block, the exception is passed to the catch block much as an argument is passed into a method parameter and, the code in the (respective) catch block is executed.

Example

 Live Demo

import java.io.File;
import java.io.FileInputStream;
public class Test {
   public static void main(String args[]){
      System.out.println("Hello");
      try{
         File file =new File("my_file");
         FileInputStream fis = new FileInputStream(file);
      }
      catch(Exception e){
         System.out.println("Given file path is not found");
      }
   }
}

Output

Given file path is not found

If the occurred exception is not handled in the catch block and thrown using the throws keyword. The code in the final block gets executed and the exceptions occur at the run time.

Example

 Live Demo

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test {
   public static void main(String args[]) throws FileNotFoundException{
      System.out.println("Hello");
      try{
         File file =new File("my_file");
         FileInputStream fis = new FileInputStream(file);
      }
      catch(ArrayIndexOutOfBoundsException e){
      }
      finally{
         System.out.println("finally block");
      }
   }
}

Output

Runtime Exception

Hello
finally block
Exception in thread "main" java.io.FileNotFoundException: my_file (The system cannot find the file specified)
   at java.io.FileInputStream.open0(Native Method)
   at java.io.FileInputStream.open(Unknown Source)
   at java.io.FileInputStream.<init>(Unknown Source)
   at sample.Test.main(Test.java:12)

Exception occurs in try-catch-finally

The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of the occurrence of an Exception.

Therefore, if an exception occurs in a try-catch-finally block. The code in the catch block, as well as finally block, gets executed.

Example

 Live Demo

public class ExcepTest {
   public static void main(String args[]) {
      int a[] = new int[2];
      try {
         System.out.println("Access element three :" + a[3]);
      }
      catch (ArrayIndexOutOfBoundsException e) {
         System.out.println("Exception thrown :" + e);
      }
      finally {
         a[0] = 6;
         System.out.println("First element value: " + a[0]);
         System.out.println("The finally statement is executed");
      }
   }
}

Output

Exception thrown : java.lang.ArrayIndexOutOfBoundsException: 3
First element value: 6
The finally statement is executed
raja
Published on 06-Sep-2019 11:40:28
Advertisements