How to print custom message instead of ErrorStackTrace in java?

Java 8Object Oriented ProgrammingProgramming

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.

Printing the Exception message

You can print the exception message in Java using one of the following methods which are inherited from Throwable class.

  • printStackTrace() − This method prints the backtrace to the standard error stream.
  • getMessage() − This method returns the detail message string of the current throwable object.
  • toString() − This message prints the short description of the current throwable object.

Example

import java.util.Scanner;
public class PrintingExceptionMessage {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter first number: ");
      int a = sc.nextInt();
      System.out.println("Enter second number: ");
      int b = sc.nextInt();
      try {
         int c = a/b;
         System.out.println("The result is: "+c);
      }catch(ArithmeticException e) {
         System.out.println("Output of printStackTrace() method: ");
         e.printStackTrace();
         System.out.println(" ");
         System.out.println("Output of getMessage() method: ");
         System.out.println(e.getMessage());
         System.out.println(" ");
         System.out.println("Output of toString() method: ");
         System.out.println(e.toString());
      }
   }
}

Output

Enter first number:
10
Enter second number:
0
Output of printStackTrace() method:
java.lang.ArithmeticException: / by zero

Output of getMessage() method:
/ by zero

Output of toString() method:
java.lang.ArithmeticException: / by zero
at PrintingExceptionMessage.main(PrintingExceptionMessage.java:11)

Printing the custom exception message

Re-throwing the exception - You can re-throw the exception catched in the catch block using new keyword, while doing this you need to pass the catched exception object along with a String representing a message, then the message you have passed is displayed instead of original message.

Example

import java.util.Scanner;
public class PrintingExceptionMessage {
   public static void main(String args[]) throws Exception {
      String msg = "This is my custom message";
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter first number: ");
      int a = sc.nextInt();
      System.out.println("Enter second number: ");
      int b = sc.nextInt();
      try {
         int c = a/b;
         System.out.println("The result is: "+c);
      }catch(ArithmeticException e) {
         throw new Exception("CoustomMessage: "+msg, e);
      }
   }
}

Output

Enter first number:
25
Enter second number:
0
Exception in thread "main" java.lang.Exception: CoustomMessage: This is my custom message
at july_set3.PrintingExceptionMessage.main(PrintingExceptionMessage.java:16)
Caused by: java.lang.ArithmeticException: / by zero
at july_set3.PrintingExceptionMessage.main(PrintingExceptionMessage.java:13)

Creating Custom Exception − You can create and re-throw a custom exception with desired message.

Example

import java.util.Scanner;
class MyException extends Exception{
   public MyException(String msg){
      super(msg);
   }
}
public class PrintingExceptionMessage {
   public static void main(String args[]) throws Exception {
      String msg = "This is my custom exception";
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter first number: ");
      int a = sc.nextInt();
      System.out.println("Enter second number: ");
      int b = sc.nextInt();
      try {
         int c = a/b;
         System.out.println("The result is: "+c);
      }catch(ArithmeticException e) {
         MyException exce = new MyException(msg);
         throw exce;
      }
   }
}

Output

Enter first number:
14
Enter second number:
0
Exception in thread "main" july_set3.MyException: This is my custom exception
   at july_set3.PrintingExceptionMessage.main(PrintingExceptionMessage.java:23)
raja
Published on 06-Aug-2019 11:31:27
Advertisements