Introduction to Exception Handling
What is an Exception?
Exception Hierarchy
In Java, all errors and exceptions are objects. The root of the exception hierarchy is the Throwable class, which belongs to the java.lang package.
Checked vs Unchecked Exceptions
In Java, exceptions are broadly classified into two categories: Checked Exceptions and Unchecked Exceptions. Understanding the difference is crucial for writing robust and compiler-friendly Java code.
Try, Catch, and Finally
Java uses five keywords for exception handling: try, catch, finally, throw, and throws. This section covers the first three, which are used to build the core mechanism of handling exceptions.
Throw vs Throws
The throw and throws keywords are used in Java exception handling, but they serve completely different purposes.
Custom (User-Defined) Exceptions
Java provides a vast array of built-in exceptions like NullPointerException and ArithmeticException. However, sometimes these standard exceptions do not accurately describe the specific business logic errors occurring in your application.