Checked exceptions — compiler forces you to handle them (try/catch or declare throws). Unchecked exceptions — compiler doesn't force handling (runtime errors).
Analogy: Checked exceptions are like required safety inspections — the compiler (government) won't let your code (building) ship until you prove you've handled them. Unchecked exceptions are like unexpected accidents — you should prepare for them, but the compiler doesn't require proof.
Checked exceptions (compile-time):
Exception (but NOT RuntimeException).throws.IOException, SQLException, ClassNotFoundException, FileNotFoundException.1// This WON'T compile without try/catch or throws:2public void readFile(String path) throws IOException {3 FileInputStream fis = new FileInputStream(path);4 // Compiler insists you handle FileNotFoundException5}67// Option 1: Catch it8public void loadConfig() {9 try {10 FileInputStream fis = new FileInputStream("config.txt");11 // read config...12 } catch (FileNotFoundException e) {13 System.err.println("Config file not found: " + e.getMessage());14 } catch (IOException e) {15 System.err.println("Error reading config: " + e.getMessage());16 }17}1819// Option 2: Declare it (propagate to caller)20public void loadConfig() throws FileNotFoundException, IOException {21 FileInputStream fis = new FileInputStream("config.txt");22 // read config...23}
Unchecked exceptions (runtime):
RuntimeException (which extends Exception).NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException, IllegalArgumentException, ClassCastException.1// These compile without try/catch (but you should still handle them!):23// NullPointerException4String str = null;5// str.length(); // Runtime: NullPointerException67// ArrayIndexOutOfBoundsException8int[] arr = new int[5];9// arr[10] = 1; // Runtime: ArrayIndexOutOfBoundsException1011// ArithmeticException12int result = 10 / 0; // Runtime: ArithmeticException: / by zero1314// IllegalArgumentException (should be thrown by developers)15public void setAge(int age) {16 if (age < 0 || age > 150) {17 throw new IllegalArgumentException("Invalid age: " + age);18 }19 this.age = age;20}2122// ClassCastException23Object obj = "hello";24// Integer num = (Integer) obj; // Runtime: ClassCastException
Exception hierarchy:
Throwable
└── Exception
├── RuntimeException (unchecked)
│ ├── NullPointerException
│ ├── ArrayIndexOutOfBoundsException
│ ├── ArithmeticException
│ ├── ClassCastException
│ └── IllegalArgumentException
├── IOException (checked)
├── SQLException (checked)
└── ClassNotFoundException (checked)
└── Error (always unchecked — don't catch these)
├── OutOfMemoryError
└── StackOverflowError
Key differences:
Exception directly.RuntimeException.Best practices:
throws Exception and ignore.Error — those are JVM-level problems (OutOfMemoryError, etc.).Common mistake: Using checked exceptions for programming errors (like throws Exception on everything). Use unchecked exceptions (IllegalArgumentException, IllegalStateException) for things that are bugs.