Exception Class in Details in Java

January 25, 2018

How do we get exception in java? Exception in thread "main" with some details popularly known as stack trace. Now if user gets such kind of exception,it is very tough for an user to understand the issue. Instead if we get a clear message , may be, the non technical person will never come to IT team.Popular exception with good exception handling--

  • Invalid username and password
  • Product not available
  • Can not divide by zero
Lets check the popular exception generation code:
Lets observe the below code:
public class myException{
public static void divide(int x,int y)
{
int z=x/y;
System.out.println(z);
}
public static void main(String args[]){
divide(10,0);
}
}
The output of the program will be as below:

Exception in thread "main" java.lang.ArithmeticException: / by zero
at myException.divide(myException.java:4)
at myException.main(myException.java:8)
It is called method stack trace.
The first line says about the type of exception-java.lang.ArithmeticException: 
The first line also says the exception message-/ by zero
And the below two lines talks about the location/package/class/method/filename where the exception really occurred. If we observe it more details, the second line talks about where the exception is actually occurred and how it is propagated to the calling function.

               ------->                ------->                                         ------->
Runtime    Call                    Call                                               Call
            --------------Main()----------------Calling Function(1)------------Calling Function(n)
System    Exception            Exception                                    Exception
              <----------             <----------                                      <----------



Whenever an exception occurred , the run time environment (JRE) generates the exception object and throws it. The moment an exception object is thrown, further exception steps and if it is not taken care of the exception propagated to the calling environment. The calling environment can be either the run time system or calling method. In the above example, the exception occurred in divide() method,as there was no handling mechanism , the exception propagated to the calling method- main().Again the main() method did not have any exception handling,it went to the calling environment i.e runtime. When runtime receives the exception,it shows the stack trace and terminate the program.
Exceptions are encapsulated into object that extends the throwable class and object of java.lang.Exception class.All the exceptions belong to Exception class which is a child of throwable class.
                                               Throwable
                                                    |
       -----------------------------------------------------------------------------
      |                                                                                                     |
 Exception                                                                                      Error(Unchecked)
      |
-------------------
|                        |
Checked       Unchecked
Checked Exception:
1. IOException
2. ClassNotFoundException
Unchecked Exception:
1. Arithmetic Exception
2. ArrayIndexOutOfBoundException
3.NullPointerException
4.ClassCastException

An exception object can make use of the below methods of the throwable class:
Method Description
String getMessage Returns the description of an exception. Example- throwable.getMessage()
Void printStackTrace() Displays the stack trace (Mostly used in debugging) Example- throwable.printStackTrace()
String toString() returns a string containing a short description of the exception Example- throwable.toString()



You Might Also Like

0 comments