Runtime Stack Mechanism in Java

For every thread JVM will create a runtime stack. All the method calls performed by the thread will be sorted in the corresponding runtime stack. If a method terminates normally the corresponding entry from the stack will be removed. After completing all the method calls the stack is empty. Just before terminating the thread JVM will destroy the corresponding stack.

Example:

class ExceptionDemo 
{
    public static void main(String[] args)
    {
        doStuff();
    }
    public static void doStuff() {
        doMoreStuff();
    }
    public static void doMoreStuff() {
        System.out.println("Hi this is Exception....Thread");
    }
}



Default Exception Handling

Example:

class ExceptionDemo 
{
    public static void main(String[] args) 
    {
        doStuff();
    }

    public static void doStuff() 
    {
        doMoreStuff();
    }

    public static void doMoreStuff()
    {
        System.out.println(10 / 0);
    }
}

Output:

Exception in thread "main" java.lang.ArithmeticExeption: / by Zero
At ExceptionDemo.doMoreStuff(ExceptionDemo.java:30)
At ExceptionDemo.doStuff(ExceptionDemo.java:25)
At ExceptionDemo.main(ExceptionDemo.java:21)

When ever an exception raised the method in which it is raised is responsible for the preparation of
exception object by including the following information 

Name of Exception.
 Description.
Location of Exception.

After preparation of Exception Object, The method handovers the object to the JVM, JVM will check for Exception handling code in that method if the method doesn’t contain any exception handling code then

JVM terminates that method abnormally and removes corresponding entry from the stack. JVM will check for exception handling code in the caller and if the caller method also doesn’t contain exception handling code then JVM terminates that caller method abnormally and removes corresponding entry from the stack. This process will be continued until main method and if the main method also doesn’t contain any exception handling code then JVM terminates main method abnormally.Just before terminating the program JVM handovers the responsibilities of exception handling to default exception handler. Default exception handler prints the error in the following format.
Name of Exception : Description
stackTrace