StackOverflowError: Thrown when the stack is exhausted due to deep recursion.Example: public static void recursiveMethod() { recursiveMethod();}try { recursiveMethod();} catch (StackOverflowError e) { System.out.println("Caught StackOverflowError: " + e.getMessage());}
InterruptedException: Thrown when a thread is interrupted while it is waiting, sleeping, or performing some other activity.Example: Thread thread = new Thread(() -> { try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println("Caught InterruptedException: " + e.getMessage()); }});thread.start();thread.interrupt();
OutOfMemoryError: Thrown when the JVM cannot allocate an object due to insufficient memory.Example: try { int[] arr = new int[Integer.MAX_VALUE];} catch (OutOfMemoryError e) { System.out.println("Caught OutOfMemoryError: " + e.getMessage());}
FileAlreadyExistsException: Thrown when an attempt is made to create a file that already exists.Example: import java.nio.file.*;import java.io.IOException;try { Path path = Paths.get("existingFile.txt"); Files.createFile(path);} catch (FileAlreadyExistsException e) { System.out.println("Caught FileAlreadyExistsException: " + e.getMessage());}
TimeoutException: Thrown when a blocking operation times out.Example: import java.util.concurrent.*;ExecutorService executor = Executors.newSingleThreadExecutor();Future future = executor.submit(() -> { Thread.sleep(2000); return "Task Completed";});try { String result = future.get(1, TimeUnit.SECONDS);} catch (TimeoutException e) { System.out.println("Caught TimeoutException: " + e.getMessage());}
ExecutionException: Thrown when attempting to retrieve the result of a task that aborted by throwing an exception.Example: ExecutorService executor = Executors.newSingleThreadExecutor();Future future = executor.submit(() -> { throw new IllegalStateException("Execution Error");});try { future.get();} catch (ExecutionException e) { System.out.println("Caught ExecutionException: " + e.getMessage());}
ClassCastException: Thrown when an object is cast to a subclass type that it is not an instance of.Example: Object obj = new String("Hello World");try { Integer number = (Integer) obj;} catch (ClassCastException e) { System.out.println("Caught ClassCastException: " + e.getMessage());}
NumberFormatException: Thrown when an attempt is made to convert a string into a number, but the string does not have an appropriate format.Example: String str = "abc";try { int number = Integer.parseInt(str);} catch (NumberFormatException e) { System.out.println("Caught NumberFormatException: " + e.getMessage());}
NullPointerException: Thrown when the JVM attempts to access a method or field of a null object.Example: String str = null;try { int length = str.length();} catch (NullPointerException e) { System.out.println("Caught NullPointerException: " + e.getMessage());}
ArithmeticException: Thrown when an illegal arithmetic operation, such as division by zero, is performed.Example: int a = 10;int b = 0;try { int result = a / b;} catch (ArithmeticException e) { System.out.println("Caught ArithmeticException: " + e.getMessage());}