Exception Handling in Java
An Overview for Engineering Students
Introduction to Exception Handling
Exception Handling in Java is a mechanism that allows the program to handle runtime errors, ensuring the
normal flow of the application. It prevents program termination due to unexpected conditions.
Definition:
An Exception is an unwanted or unexpected event that occurs during program execution, disrupting the normal
flow of instructions.
Difference Between Errors and Exceptions
Errors Exceptions
Indicate serious problems beyond control of the program (e.g., OutOfMemoryError).
Represent conditions that a program can handle (e.g., FileNotFoundException).
Cannot be recovered or handled using try-catch. Can be handled using try-catch-finally blocks.
Types of Exceptions in Java (Hierarchy)
All exceptions in Java are derived from the Throwable class. Throwable has two main subclasses: Error and
Exception. Exception is further divided into Checked and Unchecked exceptions.
Hierarchy Diagram: Throwable ■■■ Error ■■■ Exception ■■■ IOException (Checked) ■■■
SQLException (Checked) ■■■ RuntimeException (Unchecked) ■■■ NullPointerException ■■■
ArithmeticException ■■■ ArrayIndexOutOfBoundsException
Keywords in Exception Handling
The main keywords used are: try, catch, finally, throw, and throws.
1. try-catch Block
Used to handle exceptions gracefully. The try block contains risky code, and catch block handles exceptions.
Advantage over if-else: try-catch can handle multiple error types using Exception classes, while if-else can only
handle predictable conditions manually.
Example:
try { int a = 10 / 0; } catch (ArithmeticException e) { [Link]("Cannot divide by
zero."); }
Multiple catch blocks can handle different exceptions individually, and using 'catch(Exception e)' handles all
types.
2. finally Block
The finally block is used to execute important code such as resource closing, regardless of whether an
exception occurs or not.
It always executes except when [Link](0) is called.
Example:
try { FileReader file = new FileReader("[Link]"); } catch (FileNotFoundException e) {
[Link]("File not found."); } finally { [Link]("Closing file..."); }
3. throw Keyword
Used to explicitly throw an exception (either predefined or user-defined).
Example of custom exception:
class MyException extends Exception { MyException(String msg) { super(msg); } } public class
Main { public static void main(String[] args) { try { throw new MyException("Custom Error!");
} catch (MyException e) { [Link]([Link]()); } } }
4. throws Keyword
Used in method declaration to indicate that the method may throw certain exceptions, allowing the caller to
handle them.
Example:
void check() throws IOException { throw new IOException("File error"); }
Short Structure of Throwable and Exception Classes
Throwable (Superclass):
public class Throwable { private String detailMessage; public Throwable(String message) {
detailMessage = message; } public String getMessage() { return detailMessage; } }
Exception (Subclass):
public class Exception extends Throwable { public Exception(String message) {
super(message); } }
Conclusion
Exception Handling in Java provides a robust mechanism to handle runtime errors, improve code stability, and
maintain the normal flow of the application. It is an essential concept for building reliable Java programs.
Thank You!