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

Come stampare un messaggio personalizzato in Java invece di ErrorStackTrace?

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 back 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.

Esempio

import java.util.Scanner;
public class PrintingExceptionMessage {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Inserisci il primo numero:");
      int a = sc.nextInt();
      System.out.println("Inserisci il secondo numero:");
      int b = sc.nextInt();
      try {
         int c = a/b;
         System.out.println("Il risultato è: " + 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());
      }
   }
}

Risultato di output

Inserisci il primo numero:
10
Inserisci il secondo numero:
0
Output del metodo printStackTrace():
java.lang.ArithmeticException: / by zero
Output del metodo getMessage():
/ by zero
Output del metodo toString():
java.lang.ArithmeticException: / by zero
at PrintingExceptionMessage.main(PrintingExceptionMessage.java:11)

Stampa messaggio di eccezione personalizzata

Ri抛出 eccezione -Puoi utilizzare la chiave di ricerca new per ri抛出 l'eccezione catturata nel blocco catch, durante l'esecuzione di questa operazione, è necessario passare l'oggetto eccezione catturata e la Stringa che rappresenta il messaggio, quindi visualizzare il messaggio originale passato.

Esempio

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("Inserisci il primo numero:");
      int a = sc.nextInt();
      System.out.println("Inserisci il secondo numero:");
      int b = sc.nextInt();
      try {
         int c = a/b;
         System.out.println("Il risultato è: " + c);
      catch(ArithmeticException e) {
         throw new Exception("CoustomMessage: " + msg, e);
      }
   }
}

Risultato di output

Inserisci il primo numero:
25
Inserisci il secondo numero:
0
Exception in thread "main" java.lang.Exception: CoustomMessage: This is my custom message
at july_set3.PrintingExceptionMessage.main(PrintingExceptionMessage.java:16)
Causato da: java.lang.ArithmeticException: / by zero
at july_set3.PrintingExceptionMessage.main(PrintingExceptionMessage.java:13)

Creazione di eccezioni personalizzate-Puoi creare e ri抛出个人消息创建并重新抛出自定义例外。

Esempio

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("Inserisci il primo numero:");
      int a = sc.nextInt();
      System.out.println("Inserisci il secondo numero:");
      int b = sc.nextInt();
      try {
         int c = a/b;
         System.out.println("Il risultato è: " + c);
      catch(ArithmeticException e) {
         MyException exce = new MyException(msg);
         throw exce;
      }
   }
}

Risultato di output

Inserisci il primo numero:
14
Inserisci il secondo numero:
0
Exception in thread "main" july_set3.MyException: This is my custom exception
   at july_set3.PrintingExceptionMessage.main(PrintingExceptionMessage.java:23)