Exception Handling in Java
1. Error and its Types
An error is a serious issue that occurs during program execution and cannot be handled by the program.
Types of Errors:
1. Compile-time Error: Errors detected during compilation (e.g., syntax errors).
2. Run-time Error: Errors detected during execution (e.g., division by zero).
3. Logical Error: Errors due to incorrect logic (e.g., wrong formula).
2. Exception and Exception Handling
Exception: An exception is an event that disrupts the normal flow of a program.
Exception Handling: Mechanism to detect, handle, and recover from exceptions during execution without
terminating the program.
3. Types of Exceptions
a) Checked Exceptions
Handled at compile time using try-catch or throws. Example:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
b) Unchecked Exceptions
Handled at runtime. Example: int x = 10 / 0;
c) User-defined Exceptions
Custom exceptions for specific use cases. Example: class AgeException extends Exception
4. Exception Handler and its Types
Exception Handler: Block of code that handles exceptions using try, catch, finally, throw.
Types:
1. try-catch block
2. try-catch-finally block
3. Multiple catch blocks
4. Nested try-catch
5. throw keyword
6. throws keyword
Diagram: Java Exception Hierarchy
Throwable
Error Exception
Checked Exception
Unchecked Exception