Catching Multiple Exceptions with a vertical bar operator | for each of the exceptions - Java Language Basics

Java examples for Language Basics:try catch finally

Description

Catching Multiple Exceptions with a vertical bar operator | for each of the exceptions

Demo Code

import java.io.FileInputStream;
import java.io.IOException;

public class Main {
  public static void main(String[] args) {
    try {//w  w w .jav  a  2  s. c om
      Class<?> stringClass = Class.forName("java.lang.String");
      FileInputStream in = new FileInputStream("myFile.log"); // Can throw
                                                              // IOException
      in.read();

    } catch (IOException | ClassNotFoundException e) {
      System.out.println("An exception of type " + e.getClass()
          + " was thrown! " + e);
    }
  }
}

Result


Related Tutorials