Java:
Handling errors
How to:
Java uses exceptions to handle errors. You surround risky code with a try
block and catch exceptions with catch
. Here’s a simple example:
public class ErrorHandlingExample {
public static void main(String[] args) {
try {
int result = divide(10, 0);
System.out.println("Result is: " + result);
} catch (ArithmeticException e) {
System.out.println("Oops, can't divide by zero!");
}
}
private static int divide(int numerator, int denominator) {
return numerator / denominator;
}
}
Output:
Oops, can't divide by zero!
Deep Dive
Error handling in Java has evolved. Early days didn’t have exceptions; programmers checked error codes. Then Java introduced try-catch blocks, allowing more elegant error handling.
Alternatives to traditional try-catch
include try-with-resources
for auto-closing resources and cleaner code, introduced in Java 7.
Implementation details matter. For example, catching Exception
or Throwable
is usually bad practice. It’s too broad, masking bugs you might not be aware of. Stick to specific exceptions.
See Also
- The official Oracle Java tutorials on exceptions: https://docs.oracle.com/javase/tutorial/essential/exceptions/
- Java’s
try-with-resources
statement documentation: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html - Effective Java by Joshua Bloch, for best practices on exceptions.