In this article Exception Handling in Java we give the information about in Java, Exception Handling is a very powerful technique by which runtime errors are handled. Due to which the normal flow of the program is maintained.
Exception Handling in Java
-
What is an Exception?
In Java, an exception is an event that disrupts the normal flow of a program.
Whenever an exception occurs, the program execution stops and the system generate an error message.
However, we can handle these exceptions to prevent the program from crashing.
Definition:
“An exception is an unwanted or unexpected event that occurs during the execution of a program, causing a runtime error and disrupting normal flow.”
- Example:
class Exception_Test {
public static void main(String[] args) {
System.out.println(1);
System.out.println(2);
System.out.println(3);
System.out.println(4);
System.out.println(100/0); // Exception occurs here
System.out.println(5);
System.out.println(6);
}
}
Output:
1
2
3
4
Exception in thread “main” java.lang.ArithmeticException: / by zero
-
What is Exception Handling?
Exception handling in Java is a mechanism to handle runtime errors.
It ensures that the normal flow of the program is maintained even after an error occurs.
Definition:
“Exception handling is a mechanism in Java to handle runtime errors so that the normal flow of the program can be maintained.”
Analogy:
Just like an online store reships an undelivered item to handle the problem gracefully, Java handles runtime issues (exceptions) gracefully without crashing the program.
-
Difference between Exception and Error
| Sr. No. | Exception | Error |
| 1 | Caused by issues in the program logic. | Caused by lack of system resources (memory, RAM, processor). |
| 2 | Recoverable — can be handled using try-catch. | Non-recoverable — cannot be handled by the programmer. |
| 3 | Checked or Unchecked. | Always Unchecked (Runtime). |
-
Types of Exceptions
There are two main types of exceptions in Java:
- Checked Exception (Compile-Time Exception)
- Unchecked Exception (Runtime Exception)
(a) Checked Exception
- Checked by the compiler at compile-time.
- Compiler forces programmer to handle them using try-catch or throws.
- Examples:
- IOException
- FileNotFoundException
- SQLException
Example:
import java.io.File;
import java.io.FileReader;
public class FileNotFound_Demo {
public static void main(String[] args) {
File file = new File(“F://file.txt”);
FileReader fr = new FileReader(file); // Compile-time error
}
}
Output:
error: unreported exception FileNotFoundException; must be caught or declared to be thrown
(b) Unchecked Exception
- Not checked by the compiler.
- Occur due to logical/programming errors.
- Detected at runtime.
Common examples:
- NullPointerException
- ArithmeticException
- ArrayIndexOutOfBoundsException
- IllegalArgumentException
Example:
public class Unchecked_Demo {
public static void main(String[] args) {
int num[] = {1, 2, 3, 4, 5};
System.out.println(num[5]); // Runtime error
}
}
Output:
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 5
-
Difference between Checked and Unchecked Exceptions
| Sr. No. | Checked Exception | Unchecked Exception |
| 1 | Checked by the compiler at compile time. | Not checked by the compiler. |
| 2 | Must be handled using try-catch or throws. | No need to handle explicitly. |
| 3 | Common and expected exceptions. | Occur due to programming mistakes. |
| 4 | Example: IOException, SQLException. | Example: ArithmeticException, NullPointerException. |
Example of Checked Exception (File Handling):
import java.io.FileInputStream;
class CheckedExample {
public static void main(String[] args) {
try {
FileInputStream obj = new FileInputStream(“D:\\test.txt”);
} catch (Exception e) {
System.out.println(e);
}
} }
Output:
java.io.FileNotFoundException: D:\test.txt (The system cannot find the file specified)
-
Advantages of Exception Handling
- Maintains the normal flow of the program.
- Helps in identifying and categorizing errors.
- Keeps error-handling code separate from normal code.
- Increases program reliability and user experience.
- Provides clear debugging information.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining