Java Throwable initCause() 方法



描述

Java Throwable initCause() 方法將此可丟擲物件的根本原因初始化為指定的值。(根本原因是導致此可丟擲物件被丟擲的可丟擲物件。)通常在建構函式中或建立可丟擲物件後立即呼叫它。

宣告

以下是 java.lang.Throwable.initCause() 方法的宣告

public Throwable initCause(Throwable cause)

引數

cause − 這是根本原因(稍後可透過getCause() 方法檢索)。(允許為空值,表示根本原因不存在或未知。)

返回值

此方法返回對此 Throwable 例項的引用。

異常

  • IllegalArgumentException − 如果 cause 是此可丟擲物件。

  • IllegalStateException − 如果此可丟擲物件是用 Throwable(Throwable) 或 Throwable(String,Throwable) 建立的,或者此方法已在此可丟擲物件上呼叫過。

示例:獲取 Throwable 的初始原因

以下示例演示了 java.lang.Throwable.initCause() 方法的用法。在此程式中,我們定義了兩個類 CustomException 和 OtherException 來表示異常類,以及兩個方法 raiseException() 和 raiseAnotherException()。raiseException() 呼叫 raiseAnotherException(),後者依次丟擲 OtherException 作為根本異常。在 raiseException 方法的 catch 塊中,我們使用 initCause() 方法設定丟擲異常的初始原因。在主方法中,呼叫 raiseException() 方法。在 catch 塊中,我們處理異常並列印它,顯示初始原因。

package com.tutorialspoint;

public class ThrowableDemo {

   public static void main(String[] args) throws Throwable {
     try {
         raiseException();
      } catch(Exception e) {
         System.out.println(e);
      }
   }
  
   public static void raiseException()throws CustomException{
      try {
         raiseAnotherException();
      } catch(OtherException e) {
         CustomException a1 = new CustomException();
     
         // initializes the cause of this throwable to the specified value. 
         a1.initCause(e);
         throw a1;
      }
   }
  
   public static void raiseAnotherException() throws OtherException {
      throw new OtherException();
   }
}

class CustomException extends Throwable {
   CustomException() {
      super("This is my Exception....");
   }
}

class OtherException extends Throwable {
   OtherException() {
      super("This is any other Exception....");
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Exception in thread "main" com.tutorialspoint.CustomException: This is my Exception....
	at com.tutorialspoint.ThrowableDemo.raiseException(ThrowableDemo.java:17)
	at com.tutorialspoint.ThrowableDemo.main(ThrowableDemo.java:7)
Caused by: com.tutorialspoint.OtherException: This is any other Exception....
	at com.tutorialspoint.ThrowableDemo.raiseAnotherException(ThrowableDemo.java:26)
	at com.tutorialspoint.ThrowableDemo.raiseException(ThrowableDemo.java:15)
	... 1 more

示例:獲取 RuntimeException 的初始原因

以下示例演示了 java.lang.Throwable.initCause() 方法的用法。在此程式中,我們定義了一個方法 raiseException(),其中我們定義了一個 RuntimeException,並使用 initCause() 方法將初始原因設定為帶有訊息 ABCD 的可丟擲物件。在主方法中,呼叫 raiseException() 方法。在 catch 塊中,我們處理異常並列印它,顯示初始原因。

package com.tutorialspoint;

public class ThrowableDemo {

   public static void main(String[] args) throws Throwable {
     try {
         raiseException();
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
  
   public static void raiseException() throws RuntimeException {

      RuntimeException t = new RuntimeException("This is new Exception...");
      t.initCause(new Throwable("ABCD"));
      throw t;
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

java.lang.RuntimeException: This is new Exception...
	at com.tutorialspoint.ThrowableDemo.raiseException(ThrowableDemo.java:15)
	at com.tutorialspoint.ThrowableDemo.main(ThrowableDemo.java:7)
Caused by: java.lang.Throwable: ABCD
	at com.tutorialspoint.ThrowableDemo.raiseException(ThrowableDemo.java:16)
	... 1 more

示例:獲取 Exception 的初始原因

以下示例演示了 java.lang.Throwable.initCause() 方法的用法。在此程式中,我們定義了一個方法 raiseException(),其中我們定義了一個 Exception,並使用 initCause() 方法將初始原因設定為帶有訊息 ABCD 的可丟擲物件。在主方法中,呼叫 raiseException() 方法。在 catch 塊中,我們處理異常並列印它,顯示初始原因。

package com.tutorialspoint;

public class ThrowableDemo {

   public static void main(String[] args) throws Throwable {
     try {
         raiseException();
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
  
   public static void raiseException() throws Exception {

      Exception t = new Exception("This is new Exception...");
      t.initCause(new Throwable("ABCD"));
      throw t;
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

java.lang.Exception: This is new Exception...
	at com.tutorialspoint.ThrowableDemo.raiseException(ThrowableDemo.java:15)
	at com.tutorialspoint.ThrowableDemo.main(ThrowableDemo.java:7)
Caused by: java.lang.Throwable: ABCD
	at com.tutorialspoint.ThrowableDemo.raiseException(ThrowableDemo.java:16)
	... 1 more

java_lang_throwable.htm
廣告
© . All rights reserved.