A. What Are Exceptions?
(10 minutes)
1. Definition:
o Exceptions are events that disrupt the normal flow of a program’s
execution.
o In Java, exceptions are objects that represent errors or unexpected
events.
2. Purpose of Exceptions:
o To gracefully manage runtime errors.
o To improve program robustness and maintainability.
3. Code Example:
try {
int result = 10 / 0; // Will throw ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero is not allowed.");
}
B. Types of Exceptions (20 minutes)
1. Checked Exceptions:
o Must be handled during compile-time.
o Examples: IOException, SQLException.
o Code Example:
import java.io.*;
public class CheckedExample {
public static void main(String[] args) {
try {
FileReader file = new FileReader("nonexistent.txt");
} catch (IOException e) {
System.out.println("File not found.");
}
}
}
Runtime Exceptions:
Occur during program execution and do not require explicit handling.
Examples: NullPointerException, ArithmeticException.
Code Example:
public class RuntimeExample {
public static void main(String[] args) {
String str = null;
System.out.println(str.length()); // Throws NullPointerException
}
}
Errors:
Represent serious problems that applications should not try to handle.
Examples: OutOfMemoryError, StackOverflowError.
C. Writing Simple Error-Handling Code (30 minutes)
Try-Catch Blocks:
Explain the syntax and purpose:
try {
// Code that may throw an exception
} catch (ExceptionType e) {
// Handle exception
}
Finally Block:
Describe how finally ensures execution of cleanup code.
Code Example:
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[5]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index is out of bounds.");
} finally {
System.out.println("This block always executes.");
}
Custom Exceptions:
Explain how to define and use custom exceptions.
Code Example:
class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
public class CustomExceptionExample {
public static void main(String[] args) {
try {
validateAge(15);
} catch (InvalidAgeException e) {
System.out.println(e.getMessage());
}
}
static void validateAge(int age) throws InvalidAgeException {
if (age < 18) {
throw new InvalidAgeException("Age must be 18 or older.");
}
}
}
II. Practice (20 minutes)
1. Activity: Debugging Exercise
o Provide students with a Java program containing exception handling
issues.
o Ask them to identify and fix the errors.
2. Problem-Solving Task:
o Write a program that takes two integers as input and performs division.
o Ensure the program handles exceptions for division by zero and invalid
inputs.
IV. Assessment (15 minutes)
1. Quiz:
o Differentiate among checked exceptions, runtime exceptions, and
errors.
o Write a try-catch block to handle a NumberFormatException.
2. Assignment:
o Write a Java program that reads a file and handles possible exceptions,
including file not found and incorrect file format.
V. Conclusion (10 minutes)
1. Recap:
o Summarize the key concepts discussed.
o Emphasize the importance of exceptions in robust programming.
2. Q&A:
o Allow students to ask questions to clarify their understanding.
Materials Needed:
Laptop/PC with Java IDE installed
Projector
Sample Java programs for practice and demonstration