
Introduction
Exceptions in Java occur when the Java runtime system encounters an error while executing a program. When an error occurs, Java generates an exception object, which contains information about the error. This process involves multiple layers of the Java runtime environment (JRE) and Java Virtual Machine (JVM).
This document explains the internal mechanism of exception handling in Java and how exceptions propagate through the Java system.
Example: Exception Occurrence in Java
Consider the following Java program that performs division:
import java.util.Scanner;
public class ExceptionDemo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter Dividend:");
int dividend = input.nextInt();
System.out.println("Enter Divisor:");
int divisor = input.nextInt();
int result = dividend / divisor; // Exception may occur here
System.out.println(dividend + "/" + divisor + " = " + result);
System.out.println("I will be happy if I get executed");
}
}
Output when a valid divisor is entered:
Enter Dividend:
10
Enter Divisor:
2
10/2 = 5
I will be happy if I get executed
Output when the divisor is zero:
Enter Dividend:
10
Enter Divisor:
0
Exception in thread "main" java.lang.ArithmeticException: / by zero
at ExceptionDemo.main(ExceptionDemo.java:10)
In this case, Java detects the attempt to divide by zero and throws an ArithmeticException, terminating the program execution.
Internal Flow of Exception Handling in Java
Step-by-Step Breakdown of Exception Occurrence and Handling:
Step 1: Invalid Input Provided by User
When a user provides invalid input (e.g., entering 0 as a divisor), Java detects an error at runtime.
Step 2: JVM Contacts the JRE
Since the program cannot continue execution with an invalid input, the JVM contacts the Java Runtime Environment (JRE) to handle the issue.
Step 3: JRE Consults java.lang.Throwable
The JRE then interacts with the java.lang.Throwable class to determine the type of exception.
Step 4: Exception Type Determination
The Throwable class decides whether the error falls under:
Checked Exceptions (Synchronous exceptions that must be handled explicitly)
Unchecked Exceptions (Runtime exceptions like
NullPointerException,ArithmeticException, etc.)Errors (System-level errors like
StackOverflowError,OutOfMemoryError)
Step 5: JRE Contacts Java Exception API
Once the type of exception is determined, the JRE communicates with the Java Exception API to obtain an appropriate exception subclass.
Step 6: JVM Creates an Exception Object
The JVM then creates an instance of the corresponding exception subclass. For example, in the case of division by zero, an ArithmeticException object is instantiated.
Step 7: JVM Generates System Error Messages
After creating the exception object, the JVM prints a system error message on the console, which looks something like this:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at ExceptionDemo.main(ExceptionDemo.java:10)
Handling the Exception (If Implemented)
If the program includes exception handling using try-catch, the error can be caught, and a user-friendly message can be displayed instead of a system error.
Handling the Exception Using try-catch
import java.util.Scanner;
public class ExceptionHandlingDemo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter Dividend:");
int dividend = input.nextInt();
System.out.println("Enter Divisor:");
int divisor = input.nextInt();
try {
int result = dividend / divisor; // Exception handled here
System.out.println(dividend + "/" + divisor + " = " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Cannot divide by zero. Please enter a valid divisor.");
}
System.out.println("Program execution completed successfully.");
}
}
Output when divisor is zero:
Enter Dividend:
10
Enter Divisor:
0
Error: Cannot divide by zero. Please enter a valid divisor.
Program execution completed successfully.
The program now continues execution instead of crashing.
Exception Handling in JVM
Key Components Involved in Exception Handling:
JVM (Java Virtual Machine): Detects the exception and creates an exception object.
JRE (Java Runtime Environment): Determines the type of exception.
Java Exception API: Provides the appropriate exception subclass.
Programmer: Converts system error messages into user-friendly messages using
try-catch.
Hierarchy of Java Exception Handling
java.lang.Throwable
├── java.lang.Error
│ ├── StackOverflowError
│ ├── OutOfMemoryError
│
├── java.lang.Exception
├── IOException (Checked Exception)
├── SQLException (Checked Exception)
├── ArithmeticException (Unchecked Exception)
├── NullPointerException (Unchecked Exception)
5. Conclusion
When an exception occurs, the JVM, JRE, and Java Exception API work together to determine and throw the appropriate exception.
Unchecked exceptions (like
ArithmeticException) terminate program execution if not handled.Handling exceptions with
try-catchensures user-friendly messages and prevents crashes.It is recommended for Java programmers to handle exceptions properly to avoid abrupt program termination.
By understanding how exceptions occur internally, Java developers can write more robust and fault-tolerant programs.
About the author
I am a Tech Enthusiast having 13+ years of experience in 𝐈𝐓 as a 𝐂𝐨𝐧𝐬𝐮𝐥𝐭𝐚𝐧𝐭, 𝐂𝐨𝐫𝐩𝐨𝐫𝐚𝐭𝐞 𝐓𝐫𝐚𝐢𝐧𝐞𝐫, 𝐌𝐞𝐧𝐭𝐨𝐫, with 12+ years in training and mentoring in 𝐒𝐨𝐟𝐭𝐰𝐚𝐫𝐞 𝐄𝐧𝐠𝐢𝐧𝐞𝐞𝐫𝐢𝐧𝐠, 𝐃𝐚𝐭𝐚 𝐄𝐧𝐠𝐢𝐧𝐞𝐞𝐫𝐢𝐧𝐠, 𝐓𝐞𝐬𝐭 𝐀𝐮𝐭𝐨𝐦𝐚𝐭𝐢𝐨𝐧 𝐚𝐧𝐝 𝐃𝐚𝐭𝐚 𝐒𝐜𝐢𝐞𝐧𝐜𝐞. I have 𝒕𝒓𝒂𝒊𝒏𝒆𝒅 𝒎𝒐𝒓𝒆 𝒕𝒉𝒂𝒏 10,000+ 𝑰𝑻 𝑷𝒓𝒐𝒇𝒆𝒔𝒔𝒊𝒐𝒏𝒂𝒍𝒔 and 𝒄𝒐𝒏𝒅𝒖𝒄𝒕𝒆𝒅 𝒎𝒐𝒓𝒆 𝒕𝒉𝒂𝒏 500+ 𝒕𝒓𝒂𝒊𝒏𝒊𝒏𝒈 𝒔𝒆𝒔𝒔𝒊𝒐𝒏𝒔 in the areas of 𝐒𝐨𝐟𝐭𝐰𝐚𝐫𝐞 𝐃𝐞𝐯𝐞𝐥𝐨𝐩𝐦𝐞𝐧𝐭, 𝐃𝐚𝐭𝐚 𝐄𝐧𝐠𝐢𝐧𝐞𝐞𝐫𝐢𝐧𝐠, 𝐂𝐥𝐨𝐮𝐝, 𝐃𝐚𝐭𝐚 𝐀𝐧𝐚𝐥𝐲𝐬𝐢𝐬, 𝐃𝐚𝐭𝐚 𝐕𝐢𝐬𝐮𝐚𝐥𝐢𝐳𝐚𝐭𝐢𝐨𝐧𝐬, 𝐀𝐫𝐭𝐢𝐟𝐢𝐜𝐢𝐚𝐥 𝐈𝐧𝐭𝐞𝐥𝐥𝐢𝐠𝐞𝐧𝐜𝐞 𝐚𝐧𝐝 𝐌𝐚𝐜𝐡𝐢𝐧𝐞 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠. I am interested in 𝐰𝐫𝐢𝐭𝐢𝐧𝐠 𝐛𝐥𝐨𝐠𝐬, 𝐬𝐡𝐚𝐫𝐢𝐧𝐠 𝐭𝐞𝐜𝐡𝐧𝐢𝐜𝐚𝐥 𝐤𝐧𝐨𝐰𝐥𝐞𝐝𝐠𝐞, 𝐬𝐨𝐥𝐯𝐢𝐧𝐠 𝐭𝐞𝐜𝐡𝐧𝐢𝐜𝐚𝐥 𝐢𝐬𝐬𝐮𝐞𝐬, 𝐫𝐞𝐚𝐝𝐢𝐧𝐠 𝐚𝐧𝐝 𝐥𝐞𝐚𝐫𝐧𝐢𝐧𝐠 new subjects.
Comments
Join the discussionNo comments yet. Be the first to comment.


