How to find the details of exception in java
January 1, 2015
by
at
3:47 pm
Java Exception Handling Tutorial List :
1:Exception Handling in Java 2:try and catch block in Exception Handling 3:finally block in java 4:throw and throws keyword in java 5:How to find the details of exception in java 6:How to Create User Defined Exception Class 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) }
Related Articles