3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Define exceptions in Java.
- Use try-catch statements.
- Apply finally blocks.
- Use throw and throws keywords.
- Handle runtime errors in Java applications.
3.2 Overview
Exceptions are runtime errors that interrupt normal program execution. Java provides exception handling mechanisms that allow programs to detect and manage errors gracefully.
Exception handling is important in:
- banking systems,
- enterprise applications,
- database systems,
- file handling,
- and secure software development.
This lesson introduces learners to practical exception handling techniques in Java programming.
Understanding exception handling is important because reliable software must manage errors effectively.
PA2001 — Define Exceptions
Exceptions are events that interrupt normal program execution.
Common Exception Types
ArithmeticException — Division by zero
NullPointerException — Accessing null objects
ArrayIndexOutOfBoundsException — Invalid array index
NumberFormatException — Invalid number conversion
Practical Activity
Learners must:
- define exceptions,
- identify common exception types,
- and explain runtime errors.
PA2002 — Use try-catch Blocks
try-catch blocks handle exceptions safely.
Java Example:
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println(“Cannot divide by zero”);
}
Practical Activity
Learners must:
- create try-catch blocks,
- handle exceptions,
- and display error messages.
PA2003 — Use Multiple catch Blocks
Java supports multiple catch statements.
Java Example:
try {
String text = null;
System.out.println(text.length());
} catch (NullPointerException e) {
System.out.println(“Null Error”);
} catch (Exception e) {
System.out.println(“General Error”);
}
Practical Activity
Learners must:
- create multiple catch blocks,
- handle different exceptions,
- and display outputs.
PA2004 — Use finally Block
The finally block executes regardless of exceptions.
Java Example:
try {
System.out.println(“Program Running”);
} finally {
System.out.println(“Program Ended”);
}
Practical Activity
Learners must:
- apply finally blocks,
- test execution flow,
- and display outputs.
PA2005 — Use throw Keyword
The throw keyword manually creates exceptions.
Java Example:
throw new ArithmeticException(“Custom Error”);
Practical Activity
Learners must:
- create custom exceptions,
- apply throw statements,
- and display error messages.
PA2006 — Use throws Keyword
The throws keyword declares possible exceptions.
Java Example:
public void readFile() throws Exception {
}
Practical Activity
Learners must:
- declare exceptions,
- apply throws keyword,
- and identify method exceptions.
PA2007 — Handle Input Errors
Exception handling manages invalid user input.
Java Example:
try {
int number = Integer.parseInt(“ABC”);
} catch (NumberFormatException e) {
System.out.println(“Invalid Number”);
}
Practical Activity
Learners must:
- handle invalid input,
- apply parsing exceptions,
- and display outputs.
PA2008 — Create Complete Exception Handling Program
Exception handling is used in practical Java applications.
Java Example:
public class ExceptionExample {
public static void main(String[] args) {
try {
int result = 10 / 0;
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println(“Error: Division by zero”);
} finally {
System.out.println(“Program Finished”);
}
}
}
Importance of Exception Handling
Exception handling supports:
- reliable software,
- runtime error management,
- secure applications,
- and stable program execution.
3.5 Key Notes / Summary
- Exceptions interrupt normal execution.
- try-catch handles runtime errors.
- finally always executes.
- throw creates exceptions manually.
- throws declares possible exceptions.
- Exception handling improves software reliability.