Exception Handling in Java Part-1

Exception Handling in Java

In Java, the super most class of all exception classes is Throwable, which itself has 2 derive classes Exception and Error. The class Error and its derive classes represents those exceptions which are beyond the scope of a java developer.
For example:
If the user’s computer is having very low memory and we try to create a big array like int []ar= new int[100000];then in on such statement java will generate exception called OutOfMemoryError. In this case as a programmer we can do nothing. So normally we never handle exceptions which generates as sub classes of error.
Exception class is very much important for a java developer as almost every exceptions with which we are going to deal is direct or indirect derive class of class Exceptions. Some of its important sub classes are:

  1. RuntimeException
    1. ArithmeticException
    2. NumberFormatException
    3. NoSuchElementException
      1. InputMismatchException
    4. NullPointerException
    5. IndexOutOfBoundsException
      1. ArrayIndexOutOfBoundsException
      2. StringOutOfBoundsException
  2. SQLExceptions
  3. IOException
    1. FileNotFoundException
    2. EOFException
    3. MalformedURLException

Run time Stack Mechanism

  • For every thread JVM will create a Run time stack.
  • All method call perform by the thread will be store in the stack.
  • Each entry in the stack is called “Activation Record” or “Stack Frame”.
  • After completing every method call, JVM deletes the corresponding entry from the stack.
  • After completing all method calls, just before terminating the thread JVM destroys the stack.
class Example
{
    public static void main(String []args)
    {
        //making to call to another function
        doStuff();
    }
    public static void doStuff()
    {
        //making to call to another function
        doMoreStuff();
    }
    public static void doMoreStuff()
    {
        //printing a statement
        System.out.println("Don't Sleep.");
    }
}

Output:

Don't Sleep.

Default Exception Handling in Java

In any program, if any of  the exceptions is thrown then the method in which the exceptions are raised is responsible to create object of exceptions with the following information:

  1. Name of exception
  2. Description of exception
  3. Location of exception(by stack trace)

Once we create the object, method transfers the object to JVM. JVM checks whether the method contains any exception_handling code. If there exists such code then it will execute and rest of the program will execute normally. Otherwise if the caller method has no handling code then JVM will terminate the method abnormally as well as remove its entry from stack. This process will carry out till main(), but if main() also doesn’t have handling code then JVM will terminate it abnormally and remove its entry from stack. But before terminating these methods abnormally, JVM passes the responsibility of exception_handling to default exception_handler. As a result the work of this default exception_handler is that it just prints some information regarding the exceptions as:

Name of Exception: Description
Location(Stack Trace)

Consider the following example:

class SecondExample
{
    public static void main(String []args)
    {
        //making to call to another function
        doStuff();
    }
    public static void doStuff()
    {
        //making to call to another function
        doMoreStuff();
    }
    public static void doMoreStuff()
    {
        //printing a statement with exception
        System.out.println(10/0);
        //This line will throw an exception
    }
}

Output:

Exception in Thread "maim": java.lang.ArithmeticException: / by zero
at SecondExample.doMoreStuff()
at SecondExample.doStuff()
at SecondExample.main()

Exception Hierarchy

Throwable class is the super class of all types of exceptions. It has 2 child classes as:

  1. Exception: In most of the cases exceptions occur by our program , so here we can recover them with some lines of codes.
  2. Error: In most of the cases errors are not caused by our program, they are actually caused due to the lack of resources in the system therefore such errors are non recoverable.

Leave a Comment

Your email address will not be published. Required fields are marked *