Exception handling is a fundamental concept in Java that helps developers build stable and reliable applications. Understanding the types of exception in Java allows programmers to handle runtime problems gracefully and maintain program flow even when unexpected situations occur.
An exception in Java is an event that interrupts the normal execution of a program. It occurs when an error or unexpected condition arises during runtime, such as invalid user input, file access issues, or arithmetic errors.
Java exceptions follow a structured hierarchy under the Throwable class. Understanding this hierarchy is essential to effectively handle different types of exceptions.
| Class | Description |
|---|---|
| Throwable | Superclass of all errors and exceptions in Java. Only objects of this class can be thrown or caught. |
| Exception | Represents recoverable conditions. Includes both checked and unchecked exceptions. |
| RuntimeException | Subclass of Exception representing unchecked exceptions, typically caused by programming errors. |
| Error | Represents serious system-level errors, such as memory issues. Generally, these should not be handled in code. |
java.lang.Object | java.lang.Throwable / \ java.lang.Exception java.lang.Error | java.lang.RuntimeException
This hierarchy helps developers distinguish between recoverable exceptions and critical system errors, enabling better exception handling strategies.
Java follows a structured exception hierarchy, starting from the Throwable class.
| Class | Description |
|---|---|
| Throwable | Root class for all exceptions and errors |
| Exception | Recoverable exceptions |
| RuntimeException | Unchecked exceptions |
| Error | System-level errors |
Checked exceptions are verified at compile time. The Java compiler forces developers to handle these exceptions explicitly.
import java.io.File; import java.io.FileReader; public class CheckedExceptionExample { public static void main(String[] args) { try { File file = new File("data.txt"); FileReader reader = new FileReader(file); } catch (Exception e) { System.out.println("File not found"); } } }
Unchecked exceptions occur at runtime and are not checked by the compiler. These usually indicate logical errors in the program.
public class UncheckedExceptionExample { public static void main(String[] args) { int a = 10; int b = 0; int result = a / b; System.out.println(result); } }
Errors represent serious problems that applications should not try to handle.
public class ErrorExample { public static void recursiveMethod() { recursiveMethod(); } public static void main(String[] args) { recursiveMethod(); } }
Custom exceptions allow developers to define application-specific error conditions.
class InvalidAgeException extends Exception { public InvalidAgeException(String message) { super(message); } } public class CustomExceptionExample { static void validateAge(int age) throws InvalidAgeException { if (age < 18) { throw new InvalidAgeException("Age must be 18 or above"); } } public static void main(String[] args) { try { validateAge(15); } catch (InvalidAgeException e) { System.out.println(e.getMessage()); } } }
Checked exceptions are handled at compile time, while unchecked exceptions occur at runtime due to programming errors.
Errors can be caught, but it is not recommended because they indicate serious system-level problems.
Throwable is the superclass of all exceptions and errors in Java.
Custom exceptions should be used for application-specific or business rule violations.
Exception handling is mandatory for checked exceptions but optional for unchecked exceptions.
Understanding the types of exception in Java is essential for writing clean and reliable code. Checked exceptions enforce safety at compile time, unchecked exceptions reveal logical mistakes, and errors indicate critical system issues. Mastering exception handling improves code quality and application stability.
Copyrights © 2024 letsupdateskills All rights reserved