Junior
What are the key exception classes in Java?
sobes.tech AI
Answer from AI
In Java, the exception hierarchy starts with the class Throwable. It has two main subclasses: Error and Exception.
Error:
- Represents serious problems that usually should not be caught by the application.
- Occur due to the virtual machine (JVM) or environment issues.
- Examples:
OutOfMemoryError,StackOverflowError.
Exception:
- Represents conditions that the application can and should handle.
- Divided into two types:
- Checked Exceptions:
- Must be caught (
try-catch) or declared in the method signature (throws). - The compiler enforces their handling.
- Usually represent predictable but undesirable conditions that can occur at runtime.
- Examples:
IOException,FileNotFoundException,SQLException.
- Must be caught (
- Unchecked Exceptions:
- Inherit from
RuntimeException. - Do not require mandatory handling or declaration.
- Often indicate logical errors or incorrect API usage.
- Examples:
NullPointerException,ArrayIndexOutOfBoundsException,IllegalArgumentException.
- Inherit from
- Checked Exceptions:
Here are the main exception classes:
java.lang.Throwable(Base class)java.lang.Error(Unrecoverable errors)java.lang.OutOfMemoryErrorjava.lang.StackOverflowError
java.lang.Exception(Recoverable exceptions)java.io.IOException(Checked)java.io.FileNotFoundException(Checked)
java.sql.SQLException(Checked)java.lang.RuntimeException(Unchecked)java.lang.NullPointerExceptionjava.lang.IllegalArgumentExceptionjava.lang.IndexOutOfBoundsException
Comparison table:
| Feature | Checked Exception | Unchecked Exception (RuntimeException) |
Error |
|---|---|---|---|
| Inheritance | Exception (except RuntimeException and its subclasses) |
RuntimeException |
Error |
| Mandatory handling/declaration | Yes | No | No |
| Cause | Predictable failures | Programming errors, incorrect usage | Serious JVM/environment issues |
| Recovery | Often possible | Rare (indicates a bug) | Almost impossible |
Example of exception handling:
// Example of handling checked and unchecked exceptions
public class ExceptionHandlingDemo {
public void readFile(String fileName) {
try {
// Attempt to read a file - may throw FileNotFoundException (checked)
java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.FileReader(fileName));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
reader.close(); // Also may throw IOException (checked)
// Example of unchecked exception (may occur if fileName == null)
int length = fileName.length(); // NullPointerException (unchecked)
} catch (java.io.FileNotFoundException e) {
// Handle FileNotFoundException
System.err.println("Error: File not found - " + e.getMessage());
} catch (java.io.IOException e) {
// Handle general I/O errors
System.err.println("File read error: " + e.getMessage());
} catch (java.lang.NullPointerException e) {
// Handle NullPointerException - optional but good for debugging
System.err.println("Error: Null reference used - " + e.getMessage());
}
}
public static void main(String[] args) {
ExceptionHandlingDemo demo = new ExceptionHandlingDemo();
demo.readFile("non_existent_file.txt"); // Will throw FileNotFoundException
demo.readFile(null); // May throw NullPointerException
}
}