Java – Runtime mechanism, what happens when exception is thrown ?

In this article, we will discuss runtime mechanism i.e.; what happens internally when any Java program executes

  • Normally (graceful termination)
  • Abnormally (abnormal termination or exception scenario)

1. Graceful termination:

  • Sample Java program to understand runtime mechanism in a normal condition

TestRuntimeMechanismForNormalCondition.java

package in.bench.resources.exception.handling;

public class TestRuntimeMechanismForNormalCondition {

	// main() method - start of JVM execution
	public static void main(String[] args) {
		callMethodOne();
	}

	// callMethodOne() method
	public static void callMethodOne() {
		callMethodTwo();
	}

	// callMethodTwo() method
	public static void callMethodTwo() {
		callMethodThree();
	}

	// callMethodThree() method
	public static void callMethodThree() {
		System.out.println("Invoked method"
				+ " Three SUCCESSFULLY from method Two");
	}
}

Output:

Invoked method Three SUCCESSFULLY from method Two

Explanation:

  • In the above example, there is no system or programmatic exception/error
  • Therefore, program executed successfully without any exception and prints message in the console
  • Finally program terminates i.e.; graceful termination

1.1 Runtime Stack for Normal execution:

  • When program executes (normally or abnormally), JVM creates runtime stack for every thread spawned from that particular program (to store all its information like method call, etc)
  • Since, in the above example there is only one thread is spawned i.e.; main thread
  • Therefore, JVM creates one runtime stack for main thread and stores every method in the order (invoking order)
  • Each entry in the runtime stack is known as Activation Record. Alternatively, it is also called as Stack frame
  • For the above example, which executes normally JVM invokes first method call i.e.; main()
  • So, an entry in the runtime stack is stored
  • Note: generally, this is 1st method call from JVM for standalone program
  • main() method executes which has call to another static method called callMethodOne()
  • callMethodOne() method has a call to another method called callMethodTwo()
  • Similarly, callMethodTwo() method has a call to another method called callMethodThree()
  • As there is no further invocation from method three, this is last entry into the runtime stack and thus there are 4 entries in the runtime stack as shown in the above figure
  • In the reverse order, all entries into runtime stack will be exited one-by-one after corresponding method execution
  • So, callMethodThree() will be executed and its entry from runtime stack exited
  • In this way, next in line will be callMethodTwo(), callMethodOne() and finally main() method
  • Now, runtime stack won’t contain any entries i.e.; it will be empty
  • Finally, JVM destroys empty runtime stack

2. Exception scenario:

  • This sample code throws exception during execution
  • Note : this program is same as that of previous program, except we made changes to throw exception

TestRuntimeMechanismForAbnormalCondition.java

package in.bench.resources.exception.handling;

public class TestRuntimeMechanismForAbnormalCondition {

	// main() method - start of JVM execution
	public static void main(String[] args) {
		callMethodOne();
	}

	// callMethodOne() method
	public static void callMethodOne() {
		callMethodTwo();
	}

	// callMethodTwo() method
	public static void callMethodTwo() {
		callMethodThree();
	}

	// callMethodThree() method
	public static void callMethodThree() {
		int result = 19/0;
	}
}

Output:

Exception in thread "main" java.lang.ArithmeticException:/ by zero
	at in.bench.resources.exception.handling
		.TestRuntimeMechanismForAbnormalCondition.callMethodThree(
		TestRuntimeMechanismForAbnormalCondition.java:22)
	at in.bench.resources.exception.handling
		.TestRuntimeMechanismForAbnormalCondition.callMethodTwo(
				TestRuntimeMechanismForAbnormalCondition.java:17)
	at in.bench.resources.exception.handling
		.TestRuntimeMechanismForAbnormalCondition.callMethodOne(
				TestRuntimeMechanismForAbnormalCondition.java:12)
	at in.bench.resources.exception.handling
		.TestRuntimeMechanismForAbnormalCondition.main(
				TestRuntimeMechanismForAbnormalCondition.java:7)

Explanation:

  • This program is similar to the one, we have discussed in the earlier scenario i.e.; graceful termination
  • But in this exception scenario example, last method is tweaked to throw exception
  • Let us understand what happens in the runtime stack

2.1 Runtime Stack for Exception:

  • When this program is executed, similar to last program there will be 4 entries into runtime stack for each method call as there is no exception raised until 4th method
  • Now when 4th entry (i.e.; 4th method) executes, it encounters an exception and it looks for exception handling code in the same method
  • But there is no handling code in the method therefore it create exception object and passes control over to JVM along with exception object
  • JVM exits currently executing method (i.e.; 4th entry removed from stack) abnormally and looks for handler code in the caller method (i.e.; 3rd entry in the stack)
  • Since JVM doesn’t find any handling code here, so similar to last step currently executing method exits (i.e.; 3rd entry removed from stack) and method terminates abnormally
  • This continues until it reaches main() method and every entry in the runtime stack will be exited
  • Even in main method, there is no handler code therefore corresponding entry will be removed from runtime stack and main method terminates abnormally
  • Finally JVM destroys runtime stack after it becomes empty and passé the control to Default exception handler along with the exception object
  • Finally, overall program terminates abnormally and default exception handler prints exception information in the console

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - Exception Hierarchy
Java - Exception Handling