Content ITV PRO
This is Itvedant Content department
Learning Outcome
3
Apply try–catch blocks to handle exceptions in Java programs.
2
Explain the purpose and benefits of Exception Handling.
1
Understand exceptions and their types
OOP (Inheritance): Exceptions are classes; understanding parent–child relationships helps in catching parent exceptions for multiple child errors.
The Call Stack: Errors propagate backward through method calls until they are caught.
You insert your card
Enter the PIN
Type the amount.
But suddenly, something goes wrong
Everything seems fine.....
After reading the message, you understand that there is a server problem with the ATM,
So You cancel the transaction, walk out, and visit another ATM.”
The ATM displays an error message:
‘Server Not Responding’.
If the ATM simply crashed or shut down, you would feel confused and frustrated because: You don’t know what actually went wrong
Instead, the ATM displays clear error messages, such as:
“Insufficient Balance”
“Server Not Responding”
“Cash Not Available”
Because of these messages, you understand what went wrong and can decide what to do next—try another ATM, wait, or check your balance.
So just like an ATM needs a system to handle problems gracefully,
Java provides try, catch, and finally blocks to handle errors properly.
That system is called: Exception Handling in Java.
An exception is an event that occurs during program execution and causes the program to behave abnormally.
Defination
An exception is an event that occurs during program execution and causes the program to behave abnormally.
Defination
10 / 0
File Not Found
File does not exist
new FileReader("missing.txt")
Input Mismatch
Entering incorrect input
int x = scanner.nextInt() // "abc"
Arithmetic Error
Dividing a number by zero
Prevents Crash
Prevents program from abrupt termination, ensuring stability.
Maintains Flow
Ensures the normal execution flow of the application
Aids Debugging
Helps in identifying issues and troubleshooting effectively.
Separation of Concerns
Provides separate error-handling code
Exception Handling in Java is the process of handling errors that occur during program execution so that the program does not crash and can continue or exit safely.
Object
Throwable
Exception
Programmatic Errors
Error
System Errors(JVM)
Error
System Errors(JVM)
RuntimeException
Unchecked Exceptions
Other Exceptions
Checked Exceptions
Occur due to programming logic errors.
Not checked by the compiler (Runtime)
Examples
ArithmeticException
IndexOutOfBounds...
Example
Checked by the compiler at compile-time.
Mandatory handling using try-catch or throws.
SQLException
IOException
Compile-Time Error
Occurs during the compilation phase of the program.
Run-Time Errors
Occurs during the execution phase (while running).
Caused by syntax mistakes or rule violations.
Caused by logic errors or invalid data input.
Program will not run; no .class file is generated.
Program starts but crashes/terminates unexpectedly.
Detected by the Compiler (javac) automatically.
Detected by the JVM when the bad line is executed.
import java.io.FileReader;
public class Test {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("abc.txt"); // file may not exist
} catch (Exception e) {
System.out.println("File not found");
}
}
}
public class UncheckedExample {
public static void main(String[] args) {
int a = 10;
int b = 0;
int result = a / b; // runtime error
System.out.println(result);
}
}
Checked Exception
Unchecked Exception
Summary
4
Compile-time = syntax errors, Run-time = logic/data errors.
3
Types: Checked, Unchecked , and Errors
2
Exception Handling prevents crashes and maintains program flow.
1
An Exception is an error during program execution
Quiz
Which of the following is a checked
exception in Java?
A. ArithmeticException
B. NullPointerException
C. IOExceptio
D. ArrayIndexOutOfBoundsException
Which of the following is a checked
exception in Java?
A. ArithmeticException
B. NullPointerException
C. IOException
D. ArrayIndexOutOfBoundsException
Quiz-Answer
By Content ITV