IllegalArgumentException: Thrown when a method receives an illegal or inappropriate argument.Example: public class Test { public static void checkAge(int age) { if (age < 0) { throw new IllegalArgumentException("Age cannot be negative"); } } public static void main(String[] args) { try { checkAge(-1); } catch (IllegalArgumentException e) { System.out.println("Caught IllegalArgumentException: " + e.getMessage()); } }}
FileNotFoundException: Thrown when an attempt to open the file denoted by a specified pathname has failed.Example: import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;try { File file = new File("nonexistentFile.txt"); FileReader fr = new FileReader(file);} catch (FileNotFoundException e) { System.out.println("Caught FileNotFoundException: " + e.getMessage());}
ArrayIndexOutOfBoundsException: Thrown when trying to access an array with an invalid index.Example: int[] arr = new int[3];try { arr[5] = 10;} catch (ArrayIndexOutOfBoundsException e) { System.out.println("Caught ArrayIndexOutOfBoundsException: " + e.getMessage());}
NoSuchElementException: Thrown when one tries to access an element that isn’t present.Example: import java.util.Iterator;import java.util.NoSuchElementException;Iterator iterator = new ArrayList().iterator();try { iterator.next();} catch (NoSuchElementException e) { System.out.println("Caught NoSuchElementException: " + e.getMessage());}
SQLException: Thrown when there is an error in accessing a database.Example: import java.sql.*;try { Connection con = DriverManager.getConnection("jdbc:invalid:url");} catch (SQLException e) { System.out.println("Caught SQLException: " + e.getMessage());}
RemoteException: Thrown when a remote method call fails.Example: import java.rmi.RemoteException;try { throw new RemoteException("Remote exception occurred");} catch (RemoteException e) { System.out.println("Caught RemoteException: " + e.getMessage());}
SecurityException: Thrown when a security manager denies a requested permission.Example: System.setSecurityManager(new SecurityManager());try { System.getProperty("user.home");} catch (SecurityException e) { System.out.println("Caught SecurityException: " + e.getMessage());}
NullPointerException: Thrown when an application attempts to use a `null` object reference where an object is required.Example: String str = null;try { System.out.println(str.length());} catch (NullPointerException e) { System.out.println("Caught NullPointerException: " + e.getMessage());}
ArrayIndexOutOfBoundsException: Thrown when an application tries to access an array with an illegal index.Example: int[] arr = {1, 2, 3};try { System.out.println(arr[5]);} catch (ArrayIndexOutOfBoundsException e) { System.out.println("Caught ArrayIndexOutOfBoundsException: " + e.getMessage());}
ClassCastException: Thrown when an invalid cast is attempted between incompatible types.Example: Object obj = "Hello World";try { Integer i = (Integer) obj;} catch (ClassCastException e) { System.out.println("Caught ClassCastException: " + e.getMessage());}