Understanding How an Exception Occurs Internally in Java

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.
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.


