Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Hoofdstuk 14 — Foutenafhandeling (Exceptions) (robuste programma’s)

Exceptions signaleren uitzonderlijke situaties. In dit hoofdstuk leer je try/catch/finally, checked vs unchecked, throw/throws, multi-catch en try-with-resources.

Voorbeeld 1 — Basis try/catch/finally

public class Demo14_1 {
    public static void main(String[] args) {
        try {
            int x = 5 / 0;
        } catch (ArithmeticException ex) {
            System.out.println("Fout: " + ex.getMessage());
        } finally {
            System.out.println("Altijd uitgevoerd");
        }
    }
}

Voorbeeld 2 — Meerdere catches en volgorde

public class Demo14_2 {
    public static void main(String[] args) {
        try {
            Integer.parseInt("abc");
        } catch (NumberFormatException nfe) {
            System.out.println("Geen getal");
        } catch (RuntimeException re) {
            System.out.println("Runtime: " + re);
        }
    }
}

Voorbeeld 3 — Multi-catch

public class Demo14_3 {
    public static void main(String[] args) {
        try {
            if (args.length == 0) throw new IllegalArgumentException("geen args");
            String s = null; s.length();
        } catch (IllegalArgumentException | NullPointerException ex) {
            System.out.println("Probleem: " + ex.getClass().getSimpleName());
        }
    }
}

Voorbeeld 4 — throw en throws

class Checker {
    static void checkLeeftijd(int l) throws Exception {
        if (l < 18) throw new Exception("Te jong"); // checked exception
    }
}
public class Demo14_4 {
    public static void main(String[] args) {
        try {
            Checker.checkLeeftijd(15);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

Voorbeeld 5 — Aangepaste (custom) exception

class OngeldigBedragException extends RuntimeException {
    OngeldigBedragException(String msg){ super(msg); }
}
class Rekening {
    double saldo;
    void neemOp(double bedrag) {
        if (bedrag <= 0 || bedrag > saldo) throw new OngeldigBedragException("Bedrag ongeldig");
        saldo -= bedrag;
    }
}

Voorbeeld 6 — Try-with-resources (sluit automatisch)

import java.io.*;
public class Demo14_6 {
    public static void main(String[] args) throws IOException {
        try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) {
            System.out.println(br.readLine());
        }
    }
}

Voorbeeld 7 — Re-throw en context behouden

public class Demo14_7 {
    static void doeIets() {
        try {
            throw new IllegalStateException("kapot");
        } catch (IllegalStateException ex) {
            throw new RuntimeException("Higher-level context", ex);
        }
    }
}

Voorbeeld 8 — Defensieve programmeren en validatie

public class Demo14_8 {
    static int somNietNull(Integer a, Integer b) {
        if (a == null || b == null) throw new IllegalArgumentException("a/b mag niet null zijn");
        return a + b;
    }
}

Tips:

  • Vang alleen exceptions die je daadwerkelijk kunt afhandelen. Laat de rest doorstijgen.
  • Plaats zo klein mogelijke try-blokken, enkel rond code die kan falen.
  • Gebruik try-with-resources voor I/O zodat resources altijd gesloten worden.

Koppeling met oefeningen:

  • Zie java-workbook/src/chapter_14.md voor opdrachten met try/catch en custom exceptions.
  • Uitwerkingen in java-solutions/src/chapter_14.md en Java-bestanden.