What is the catch block in Java?

Java 8Object Oriented ProgrammingProgramming

A catch statement 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.

Example

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

Hello
Given file path is not found
raja
Published on 08-Feb-2018 09:42:30
Advertisements