Home » Java » How to find the details of exception in java

How to find the details of exception in java

Number of ways to find details of the exception :

In JAVA there are three ways to find the details of the exception. They are using an object of java.lang.Exception class, public void printStackTrace method and using public string getMessage method.

1. Using an object of java.lang.Exception :

An object of Exception class prints the name of the exception and nature of the message.

For example :

try
{
int x=Integer.parseInt ("10x");
}
catch (Exception e)
{
System.out.println (e);// java.lang.NumberFormatException(name of the exception) 
}                      //for input string 10x(nature of the message)
}

2. Using printStackTrace method :

This method is defined in java.lang.Throwable class and it is inherited into java.lang.Error and java.lang.Exception class. This method will display name of the exception, nature of the message and line number where the exception has occured.

For example :

try
{
......;
int x=10/0;
......;
}
catch (Exception e)
{
e.printStackTrace ();// java.lang.ArithmeticException(name of the exception)
}                    // by zero(nature of the message) 
                     // at line no: 4(line number)

3. Using getMessage method :

This method is defined in java.lang.Throwable class and it is inherited into both Error and Exception classes. This method will display only nature of the message.

CALLED FUNCTION

try
{
......;
int x=10/0;
......;
}
catch (Exception e)
{
System.out.println (e.getMessage ()); // / by zero(nature of the message)
}

Previous Next Article

comments powered by Disqus