Home » Java programming language

Different ways to print Exception message in Java

Printing exception messages in Java: Here, we are going to learn the different ways to print Exception message in Java?
Submitted by Preeti Jain, on July 21, 2019

Whenever exception throw then Throwable class provides various methods to provide exception related information like Exception name, Exception description and Stack Trace, etc.

We will discuss three methods of Throwable class which provides exception related information so the name of these methods are:

  1. printStackTrace() method
  2. toString() method
  3. getMessage() method

We will see what is the purpose of these methods and how it works...

1) printStackTrace() method

  • This method is available in the package java.lang.Throwable.printStackTrace().
  • This method provides exception related information and we will see which information this method will provide.
    • Name of the Exception
    • Description of the Exception
    • Stack Trace of the Exception

Syntax:

    Name of the Exception : Description of the Exception
    Stack Trace of the Exception

Example:

class PrintStackTrace {

    public static void main(String[] args) {

        Object obj = null;

        try {
            System.out.println(obj.toString());
        } catch (Exception ex) {
            /*Display exception name : exception description
            Stack trace */
            ex.printStackTrace();
        }

    }

}

Output

E:\Programs>javac PrintStackTrace.java
E:\Programs>java PrintStackTrace
java.lang.NullPointerException
        at PrintStackTrace.main(PrintStackTrace.java:8)

2) toString() method

  • This method is available in the package java.lang.Throwable.toString().
  • This method also provides exception related information and we will see again which information this method will provide.
    • Name of the Exception
    • Description of the Exception

Syntax:

    Name of the Exception : Description of the Exception

Example:

class ToStringMethod {

    public static void main(String[] args) {

        try {
            int i = 10 / 0;
            System.out.println(i);
        } catch (Exception ex) {
            // Display exception name : exception description		
            System.out.println(ex.toString());
        }

    }

}

Output

E:\Programs>javac ToStringMethod.java

E:\Programs>java ToStringMethod
java.lang.ArithmeticException: / by zero

3) getMessage() method

  • This method is also available in the package java.lang.Throwable.printStackTrace().
  • This method provides exception related information and we will see which information this method will provide.
    Description of the Exception
  • This method does not provide other information like exception name and exception stack trace.

Syntax:

    Description of the Exception

Example:

class GetMessageMethod {

    public static void main(String[] args) {

        try {
            int i = 10 / 0;
            System.out.println(i);
        } catch (Exception ex) {
            // Display exception description
            System.out.println(ex.getMessage());
        }

    }

}

Output

E:\Programs>javac GetMessageMethod.java

E:\Programs>java GetMessageMethod
/ by zero






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.