English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to print a custom message instead of ErrorStackTrace in Java?

Exceptions are problems that occur during program execution (runtime errors). When an exception occurs, the program terminates abruptly, and the code after the line where the exception was generated will never be executed.

Print exception messages

You can use one of the following methods inherited from the Throwable class to print exception messages in Java.

  • printStackTrace() -This method prints the stack trace to the standard error stream.

  • getMessage() -This method returns a detailed message string of the current throwable object.

  • toString() -This message displays a brief 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;
         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 Result

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 a custom exception message

Rethrowing an exception-You can rethrow an exception caught in a catch block using the new keyword. When doing this, you need to pass the caught exception object along with the String representing the message, and then display the original message passed.

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;
         c);+catch(ArithmeticException e) {
      }
         throw new Exception("CoustomMessage: "}+msg, e);
      }
   }
}

Output Result

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 a custom exception-You can create and rethrow a custom exception with the required 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;
         c);+catch(ArithmeticException e) {
      }
         MyException exce = new MyException(msg);
         throw exce;
      }
   }
}

Output Result

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)