Sei sulla pagina 1di 6

6/7/2016

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

Posted date: Apr 25, 2016

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.

1.Using an object of java.lang.Exception


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

Write a Java program get detailed message details using exception class object

01. package exceptions;


02. public class ExceptionDetails {
03.
04. /**
05. * @www.instanceofjava.com
06. **/
http://www.instanceofjava.com/2016/04/waystofinddetailsofexceptionjava.html

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

3. Java Source Code

5. Java Job

2. Learn Java

4. Java Books

6. Java Programming

2.Using public void printStackTrace() method

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

01. package exceptions;


02. public class ExceptionDetails {
03.
04. /**
05. * @www.instanceofjava.com
06. */
07. public static void main(String[] args) {
08.
09.
10. try {
11. int a[]= new int[1];
12. a[1]=12
13.
14. } catch (Exception e) {
15. e.printStackTrace();
16.
17. }
18. }
19.
http://www.instanceofjava.com/2016/04/waystofinddetailsofexceptionjava.html

2/6

6/7/2016

3differentwaystoprintexceptionmessageinjavaInstanceOfJava

19.
20.

Output:

01.

java.lang.ArrayIndexOutOfBoundsException: 1
at exceptions.ExceptionDetails.main(ExceptionDetails.java:13)

3.Using public String getMessage() method


This is also a method which is defined in java.lang.Throwable class and it is inherited in to both java.lanf.Error and
java.lang.Exception classes.
This method will display the only exception message

Write a Java program print exception message using getMessage() method.

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:

exception handling interview questions

1. Java Coding Course

6. Java Job

2. Coding With Java

7. Java Tutorials

3. Java Tutorial for Programmers

8. Java Architect

4. Projects in Java

9. Java Programming Training

5. Thinking in Java

10. Java Certification

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

Top 20 Java Exception handling interview


questions and answers

throw keyword in java with example

RelatedPosts
Top 20 Java Exception handling interview
questions and answers

throw keyword in java with example


25 Apr 2016

26 Apr 2016

Multiple catch blocks in java example

Can we have try without catch block in java

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

Core Java Tutorial

Concept and Interview Question

Servlet Tutorial

concept and program

0-3 years experience Java interview


questions and answers for Freshers and
experienced

Core java Interview Questions

Java Programs asked in Interviews

Find Output of Program

Java Collections Tutorial

Java 8

Jsp Tutorial

Java EE

Concept and Example Programs

Java Programs

Concept and Interview Questions

Java SE

What is out put of following program

Jsp

Design patterns in java

Oracle

Register With Us

Servlets Tutorial

Design Pattern

Copyright 2014-2015 instanceofjava.com , All rights reserved | Privacy policy | About Us

http://www.instanceofjava.com/2016/04/waystofinddetailsofexceptionjava.html

6/6

Potrebbero piacerti anche