public class DivideByZeroExceptionExample { public static void main(String[] args) { try { int a = 10; int b = 0; int c = a / b; System.out.println("Result: " + c); } catch (ArithmeticException e) { System.out.println("Cannot divide by zero"); } } }
public class FileNotFoundExceptionExample { public static void main(String[] args) { try { File file = new File("myfile.txt"); Scanner scan = new Scanner(file); while (scan.hasNextLine()) { System.out.println(scan.nextLine()); } } catch (FileNotFoundException e) { System.out.println("File not found"); } } }
public class ArrayExample { public static void main(String[] args) { int[] arr = {1, 2, 3}; try { System.out.println(arr[3]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Index out of bounds"); } } }This example demonstrates how to handle an ArrayIndexOutOfBoundsException by catching the exception and printing a custom error message. Package Library: Java standard library.