Exception handling is fundamental to building reliable, robust applications . It enables graceful error recovery, prevents application crashes, and provides meaningful error information.
An exception is an event that disrupts the normal flow of the program
It is an object that describes an error condition
Exception handling allows programs to recover gracefully from errors
Category Description Examples Checked Checked at compile time IOException, SQLExceptionUnchecked Checked at runtime NullPointerException, ArrayIndexOutOfBoundsExceptionErrors Serious issues, not recoverable OutOfMemoryError, StackOverflowError
Feature Checked Unchecked Compile-time check ✅ Yes ❌ No Must handle Yes (try-catch or throws) Optional Typical use File I/O, DB access, Network Logic bugs, NullPointer Superclass ExceptionRuntimeExceptionRecovery possible Often yes Usually no
├── Error (Unchecked, JVM-level)
│ ├── VirtualMachineError
├── Checked Exception (must handle)
│ ├── ClassNotFoundException
└── Unchecked Exception (RuntimeException & subclasses)
├── IllegalArgumentException
├── ArrayIndexOutOfBoundsException
├── NumberFormatException
└── ConcurrentModificationException
Type When Thrown Must Handle Example Error JVM/system level ❌ No OutOfMemoryErrorChecked Exception Application logic ✅ Yes IOExceptionUnchecked Exception Runtime logic errors ❌ No NullPointerException
Use try to wrap code that might throw exceptions.
} catch ( ArithmeticException e ) {
System . out . println ( " Cannot divide by zero! " ) ;
System . out . println ( " Always executes " ) ;
try : Monitors the block for exceptions
catch : Handles specific exception types
finally : Always executes, used for cleanup (like closing files)
System . out . println ( arr[ 10 ] ) ;
} catch ( ArrayIndexOutOfBoundsException e ) {
System . out . println ( " Index out of range! " ) ;
System . out . println ( " Some error occurred " ) ;
public class MultipleCatchExample {
public static void process ( String input ) {
int number = Integer . parseInt ( input ) ;
int result = 100 / number;
System . out . println ( " Result: " + result ) ;
} catch ( NumberFormatException e ) {
System . err . println ( " Invalid number format: " + input ) ;
} catch ( ArithmeticException e ) {
System . err . println ( " Division by zero " ) ;
System . err . println ( " Unexpected error: " + e . getMessage ()) ;
public static void multiCatch ( String input ) {
int number = Integer . parseInt ( input ) ;
int result = 100 / number;
System . out . println ( " Result: " + result ) ;
} catch ( NumberFormatException | ArithmeticException e ) {
System . err . println ( " Input error: " + e . getMessage ()) ;
Used when a method declares it might throw an exception
Moves responsibility to the caller
Caller must handle the exception or declare it further
public class ThrowsExample {
public void readFile () throws IOException {
FileReader fr = new FileReader ( " file.txt " ) ;
public void processFile () throws IOException , SQLException {
readFile () ; // This method throws IOException
// Database operations that might throw SQLException
// Caller must handle exceptions
public static void main ( String [] args ) {
ThrowsExample example = new ThrowsExample () ;
} catch ( IOException | SQLException e ) {
System . err . println ( " Error: " + e . getMessage ()) ;
public class ThrowExample {
public static void validateAge ( int age ) {
throw new ArithmeticException ( " Not eligible to vote " ) ;
throw new IllegalArgumentException ( " Age cannot be negative " ) ;
System . out . println ( " Valid age: " + age ) ;
public static void main ( String [] args ) {
validateAge ( 15 ) ; // Will throw exception
} catch ( ArithmeticException e ) {
System . err . println ( " Validation failed: " + e . getMessage ()) ;
public class CustomValidation {
public static void validateEmail ( String email ) {
if (email == null || ! email . contains ( " @ " ) ) {
throw new IllegalArgumentException ( " Invalid email format: " + email ) ;
System . out . println ( " Valid email: " + email ) ;
class AgeException extends Exception {
AgeException ( String msg ) {
public class CustomExceptionExample {
public static void validateAge ( int age ) throws AgeException {
throw new AgeException ( " Invalid Age: " + age ) ;
throw new AgeException ( " Age seems unrealistic: " + age ) ;
System . out . println ( " Valid age: " + age ) ;
public static void main ( String [] args ) {
validateAge ( - 5 ) ; // Will throw AgeException
} catch ( AgeException e ) {
System . err . println ( " Age validation failed: " + e . getMessage ()) ;
Exception Description Example NullPointerExceptionAccessing a null object reference String str = null; str.length();ArrayIndexOutOfBoundsExceptionInvalid array index int[] arr = new int[5]; arr[10];ClassNotFoundExceptionClass not found at runtime Class.forName("NonExistentClass");IllegalArgumentExceptionInvalid method argument Integer.parseInt("abc");NumberFormatExceptionInvalid conversion Integer.parseInt("abc");ArithmeticExceptionMathematical error int result = 10 / 0;ClassCastExceptionInvalid type casting Object obj = "String"; Integer num = (Integer) obj;
Catch only specific exceptions - avoid catching generic Exception
Always clean up with finally or try-with-resources
Avoid empty catch blocks - always handle or log exceptions
Don’t use exceptions for flow control - they’re expensive
Log exceptions properly with context information
Rethrow exceptions when appropriate to maintain the call stack