Exception Handling Java
Exception Handling Java
1. What is an Exception?
An exception is an event that disrupts the normal flow of a program's instructions.
2. Types of Exceptions
• Checked Exceptions
• Unchecked Exceptions
• Errors
3. Checked Exceptions
Handled during compile-time.
Examples: IOException, SQLException
4. Unchecked Exceptions
Handled during runtime.
Examples: NullPointerException, ArithmeticException
5. Try-Catch Block
Syntax:
try {
// code
} catch (ExceptionType name) {
// handler
}
6. Finally Block
Executes after try-catch.
Used for cleanup operations like closing files.
7. Throw Keyword
Used to explicitly throw an exception.
Example: throw new ArithmeticException("error")
8. Throws Keyword
Declares exceptions in method signature.
Example: public void read() throws IOException
9. Custom Exceptions
You can define your own exceptions by extending the Exception class.