Professional Documents
Culture Documents
3differentwaystoprintexceptionmessageinjavaInstanceOfJava
Home
Phone Interview
Java SE
Java EE
Concept-Program
Interview Questions
Concept-Qsns
Programs
Jobs
Java Quiz
More..
Home exception handling interview questions 3 different ways to print exception message in java
3differentwaystoprintexceptionmessageinjava
Posted by: InstanceOfJava
comment : 0
In Java there are three ways to find the details of the exception .
They are
1. Using an object of java.lang.Exception
2. Using public void printStackTrace() method
3. Using public String getMessage() method.
Write a Java program get detailed message details using exception class object
1/6
6/7/2016
3differentwaystoprintexceptionmessageinjavaInstanceOfJava
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
**/
public static void main(String[] args) {
try {
int x=1/0;
} catch (Exception e) {
System.out.println(e);
}
Output:
01.
java.lang.ArithmeticException: / by zero
1. Java Tutorials
5. Java Job
2. Learn Java
4. Java Books
6. Java Programming
This is the method which is defined in java.lang.Throwable class and it is inherited into java.lang.Error class and
java.lang.Exception class
This method will display the name of the exception and nature of the message and line number where exception
has occurred.
Write a simple java example program to print exception message to console using printStacktrace() method
2/6
6/7/2016
3differentwaystoprintexceptionmessageinjavaInstanceOfJava
19.
20.
Output:
01.
java.lang.ArrayIndexOutOfBoundsException: 1
at exceptions.ExceptionDetails.main(ExceptionDetails.java:13)
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
package exceptions;
public class ExceptionDetails {
/**
* @www.instanceofjava.com
*/
public static void main(String[] args) {
try {
int x=1/0;
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
Output:
01.
/ by zero
http://www.instanceofjava.com/2016/04/waystofinddetailsofexceptionjava.html
3/6
6/7/2016
3differentwaystoprintexceptionmessageinjavaInstanceOfJava
Share!
Tagged with:
6. Java Job
7. Java Tutorials
8. Java Architect
4. Projects in Java
5. Thinking in Java
InstanceOfJava
We will help you in learning.Please leave your comments and suggestions in comment section. We have provided search box
you can serch there for answers
http://www.instanceofjava.com/2016/04/waystofinddetailsofexceptionjava.html
4/6
6/7/2016
3differentwaystoprintexceptionmessageinjavaInstanceOfJava
Next
Previous
RelatedPosts
Top 20 Java Exception handling interview
questions and answers
26 Apr 2016
25 Apr 2016
25 Apr 2016
No comments
Leave a Reply
Enteryourcomment...
Commentas:
Publish
Selectprofile...
Preview
Newer Post
http://www.instanceofjava.com/2016/04/waystofinddetailsofexceptionjava.html
Home
Older P
5/6
6/7/2016
3differentwaystoprintexceptionmessageinjavaInstanceOfJava
FollowbyEmail
Emailaddress...
Submit
Pages
Labels
Home
Collections
Servlet Tutorial
Java 8
Jsp Tutorial
Java EE
Java Programs
Java SE
Jsp
Oracle
Register With Us
Servlets Tutorial
Design Pattern
http://www.instanceofjava.com/2016/04/waystofinddetailsofexceptionjava.html
6/6