Java Exception Handling – Quick Revision Sheet
1. What is an Exception?
• An unwanted event disrupting program flow
• Occurs at runtime
• Example: divide by zero, invalid index
2. Types of Exceptions
• Checked – compile-time (IOException)
• Unchecked – runtime (ArithmeticException)
• Errors – serious (OutOfMemoryError)
3. Keywords
• try – risky code
• catch – handle exception
• finally – always executes
• throw – create exception
• throws – declare exception
4. try-catch Example
try {
int a = 10/0;
} catch(Exception e){
[Link]("Error");
}
5. Multiple Catch
try {
int arr[] = new int[2];
arr[5]=10;
} catch(ArithmeticException e){
[Link]("Arithmetic");
} catch(ArrayIndexOutOfBoundsException e){
[Link]("Array Error");
} catch(Exception e){
[Link]("General");
}
6. finally Block
finally {
[Link]("Always runs");
}
7. throw & throws
throw new ArithmeticException();
void test() throws IOException {}
8. Key Points
• Only one catch executes
• Order: specific → general
• finally always runs
• Checked must be handled
• Unchecked optional
Memory Trick
• Try Catch Finally Throw Throws (T-C-F-T-T)
• One try, many catches, first match executes