如何在 Java 中列印自定義訊息而不是 ErrorStackTrace?


異常是在程式執行過程中發生的錯誤(執行時錯誤)。當發生異常時,程式會突然終止,並且異常行之後的程式碼將永遠不會執行。

列印異常訊息

您可以使用以下繼承自 Throwable 類的其中一種方法在 Java 中列印異常訊息。

  • printStackTrace() − 此方法將回溯資訊列印到標準錯誤流。
  • getMessage() − 此方法返回當前可丟擲物件の詳細訊息字串。
  • toString() − 此訊息列印當前可丟擲物件的簡短描述。

示例

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());
      }
   }
}

輸出

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)

列印自定義異常訊息

重新丟擲異常 - 您可以使用 new 關鍵字在 catch 塊中重新丟擲捕獲的異常。在此過程中,您需要將捕獲的異常物件以及表示訊息的字串一起傳遞,然後顯示您傳遞的訊息而不是原始訊息。

示例

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);
      }
   }
}

輸出

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)

建立自定義異常 − 您可以建立並重新丟擲一個具有所需訊息的自定義異常。

示例

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;
      }
   }
}

輸出

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)

更新於: 2020年7月2日

3K+ 瀏覽量

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.