This episode covers error handling in Java: the structure of checked and unchecked exceptions, try-catch-finally and try-with-resources blocks, rethrowing exceptions, creating custom exceptions, and simple debugging techniques in the IDE plus reading stack traces.

A good program does not only run when everything is normal — it must also stay stable when errors occur. Episode 7 covers exception handling and basic debugging, the skills that separate beginner developers from professional ones.
Java has a structured exception system. You will learn to distinguish checked and unchecked exceptions, handle errors with try-catch, clean up resources with try-with-resources, create custom exceptions, and debug programs by reading stack traces and using IDE features.
A checked exception is one that the compiler requires you to handle or declare. Examples are IOException and SQLException. The compiler forces you to write try-catch or add throws:
import java.nio.file.*;
public class BacaFile {
public static void main(String[] args) throws IOException {
String isi = Files.readString(Path.of("data.txt"));
System.out.println(isi);
}
}Without handling, the code above will not compile.
An unchecked exception is a subclass of RuntimeException that does not have to be handled. Examples are NullPointerException, ArithmeticException, and IllegalArgumentException. The compiler does not force you, but handling them is still important:
public class Demo {
public static void main(String[] args) {
int hasil = 10 / 0; // ArithmeticException saat runtime
System.out.println(hasil);
}
}The classic exception handling structure consists of try, catch, and finally. The finally block always executes, whether an error occurs or not:
public static void proses() {
try {
int hasil = 10 / 0;
System.out.println(hasil);
} catch (ArithmeticException e) {
System.out.println("Tidak bisa membagi nol");
} finally {
System.out.println("Selesai memproses");
}
}Java 7 introduced try-with-resources to close resources automatically. Resources that implement AutoCloseable are closed by themselves:
import java.io.*;
import java.nio.file.*;
public class Baca {
public static void main(String[] args) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(Path.of("data.txt"))) {
System.out.println(reader.readLine());
}
}
}try (BufferedReader reader = ...) guarantees the reader is closed automatically, even when an exception occurs in the middle of the block.
Rethrowing means catching an exception and then throwing it again, sometimes with added context or after logging:
public static void proses() throws IOException {
try {
bacaData();
} catch (IOException e) {
System.out.println("Log: gagal baca " + e.getMessage());
throw e;
}
}For specific business errors, create a custom exception by extending RuntimeException or Exception:
public class SaldoTidakCukupException extends RuntimeException {
public SaldoTidakCukupException(String pesan) {
super(pesan);
}
}Custom exceptions make your code more expressive and make it easier to handle specific errors:
if (saldo < jumlah) {
throw new SaldoTidakCukupException("Saldo " + saldo + " tidak mencukupi");
}When an exception is not handled, the JVM prints a stack trace — the chain of method calls from the error point back to the start. Reading it is an essential skill:
Exception in thread "main" java.lang.NullPointerException
at com.aplikasi.Order.hitungTotal(Order.java:42)
at com.aplikasi.Main.main(Main.java:8)The top line shows the exception type, and the following lines show the exact location — class, method, and line number. Start debugging from the first line that references your own code.
Modern IDEs such as IntelliJ IDEA have a visual debugger. The basic techniques:
The debugger lets you observe the program state live — far more effective than guessing from output.
Episode 7 equips you with error handling: distinguishing checked and unchecked exceptions, using try-catch-finally and try-with-resources, rethrowing, creating custom exceptions, and debugging by reading stack traces and using IDE debugger features.
Key takeaways:
In the next episode, episode 8, we will discuss input/output and file management — the basics of Java I/O with java.io and java.nio, reading and writing text, binary, and CSV files, the Path API, Files utility, and working directory, and best practices for resource management with try-with-resources.