LESSON 20
3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Define exceptions in Java.
- Explain the purpose of exception handling.
- Use try-catch blocks in Java applications.
- Use finally blocks in exception handling.
- Handle common runtime exceptions in Java.
3.2 Overview
Errors may occur during program execution due to invalid input, mathematical problems, missing files, or incorrect operations. Java provides exception handling mechanisms that allow applications to manage runtime errors gracefully without crashing.
This lesson introduces learners to exception handling in Java and explains how exceptions are detected, handled, and managed within Java applications. Learners will also explore try-catch-finally structures and common runtime exceptions.
Exception handling is commonly used in:
- banking systems,
- healthcare applications,
- enterprise software,
- inventory systems,
- and web applications.
Understanding exception handling is important because it improves application reliability, stability, and user experience.
KT2001 — Introduction to Exceptions
An exception is an error or unexpected event that occurs during program execution.
Exceptions interrupt the normal flow of a program.
Examples of Exceptions
Common exceptions include:
- division by zero,
- invalid array indexes,
- invalid input,
- and missing files.
Importance of Exception Handling
Exception handling helps developers:
- prevent application crashes,
- manage runtime errors,
- improve stability,
- and provide better user experience.
KT2002 — The try-catch Block
Java uses try-catch blocks to handle exceptions.
The try block contains code that may generate exceptions.
The catch block handles the exception.
Syntax
try {
// risky code
} catch(ExceptionType e) {
// handling code
}
Example
public class Main {
public static void main(String[] args) {
try {
int result = 10 / 0;
System.out.println(result);
} catch(ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
}
}
Output
Cannot divide by zero
Importance of try-catch
try-catch blocks help:
- manage runtime errors,
- prevent abrupt termination,
- and improve application reliability.
KT2003 — The finally Block
The finally block executes regardless of whether an exception occurs.
finally blocks are commonly used for cleanup operations.
Example
public class Main {
public static void main(String[] args) {
try {
int number = 10 / 0;
} catch(ArithmeticException e) {
System.out.println("Error occurred");
} finally {
System.out.println("Execution completed");
}
}
}
Output
Error occurred
Execution completed
Importance of finally
finally blocks are used for:
- closing resources,
- cleanup processing,
- and guaranteed execution tasks.
KT2004 — Common Exceptions in Java
Java provides many built-in exception classes.
Common Exceptions
| Exception | Cause |
|---|---|
| ArithmeticException | Division by zero |
| ArrayIndexOutOfBoundsException | Invalid array index |
| NullPointerException | Accessing null object |
| NumberFormatException | Invalid number conversion |
Example
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
try {
System.out.println(numbers[5]);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Invalid array index");
}
}
}
Output
Invalid array index
Importance of Common Exceptions
Understanding common exceptions helps developers:
- debug applications,
- prevent failures,
- and improve software quality.
KT2005 — The throw Keyword
The throw keyword manually generates exceptions.
Example
public class Main {
public static void main(String[] args) {
int age = 15;
if(age < 18) {
throw new ArithmeticException("Access denied");
}
}
}
Output
Exception in thread "main" java.lang.ArithmeticException: Access denied
Importance of throw
The throw keyword helps developers:
- validate data,
- enforce rules,
- and generate custom errors.
KT2006 — Real-World Use of Exception Handling
Exception handling is used in:
- banking systems,
- ATM systems,
- healthcare applications,
- web applications,
- and enterprise software.
Examples:
- validating transactions,
- handling invalid input,
- processing failed logins,
- and managing system errors.
Exception handling improves application stability and reliability.
3.5 Key Notes / Summary
- Exceptions are runtime errors or unexpected events.
- try blocks contain risky code.
- catch blocks handle exceptions.
- finally blocks execute regardless of exceptions.
- Java provides built-in exception classes.
- throw manually generates exceptions.
- Exception handling improves application reliability.