data-flair.training/blogs exception code example
Example 1: data-flair.training/blogs exception
try {
//Code where exception can occur
}
catch(ExceptionTypeA e1) {
//code that executes if Exception of type A occurs
}
catch(ExceptionTypeB e2) {
//code that executes if Exception of type B occurs.
}
Example 2: data-flair.training/blogs exception
package com.dataflair.javaexceptions;
import java.io. * ;
public class CheckUnCheck {
public static void main(String[] args) throws IOException {
try {
System.out.println(25 / 0);
System.out.println("This statement will never get executed because the control has shifted to the catch block. ");
}
catch(Exception e) {
System.out.println("You are trying to divide by zero! That is not right!");
}
finally {
System.out.println("This code is in the finally block. It does not depend on whether an exception occurs or not. ");
}
}
}