當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Throwable initCause()用法及代碼示例


Throwable類的initCause()方法用於初始化此Throwable的原因,並將指定的原因作為參數傳遞給initCause()。實際上,原因是引發異常時導致拋出此對象的可拋出對象。此方法隻能調用一次。通常,從構造函數內部或在創建throwable之後立即調用此方法。如果調用Throwable是使用Throwable(Throwable)或Throwable(String,Throwable)創建的,則即使調用一次此方法也不能。

用法:

public Throwable initCause?(Throwable cause)

參數:此方法接受cause作為表示此Throwable原因的參數。


返回值:此方法返回對此Throwable實例的引用。

異常:該方法拋出:

  • IllegalArgumentException如果原因是可拋出的。
  • IllegalStateException如果此Throwable是使用Throwable(Throwable)或Throwable(String,Throwable)創建的,或者已經在此throwable上調用了此方法。

以下程序說明了Throwable類的initCause方法:

示例1:

// Java program to demonstrate 
// the initCause() Method. 
  
import java.io.*; 
  
class GFG { 
  
    // Main Method 
    public static void main(String[] args) 
        throws Exception 
    { 
  
        try { 
  
            testException1(); 
        } 
  
        catch (Throwable e) { 
  
            System.out.println("Cause : "
                               + e.getCause()); 
        } 
    } 
  
    // method which throws Exception 
    public static void testException1() 
        throws Exception 
    { 
  
        // ArrayIndexOutOfBoundsException Exception 
        // This exception will be used as a Cause 
        // of another exception 
        ArrayIndexOutOfBoundsException 
            ae 
            = new ArrayIndexOutOfBoundsException(); 
  
        // create a new Exception 
        Exception ioe = new Exception(); 
  
        // initialize the cause and throw Exception 
        ioe.initCause(ae); 
  
        throw ioe; 
    } 
}
輸出:
Cause : java.lang.ArrayIndexOutOfBoundsException

示例2:

// Java program to demonstrate 
// the initCause() Method. 
  
import java.io.*; 
  
class GFG { 
  
    // Main Method 
    public static void main(String[] args) 
        throws Exception 
    { 
  
        try { 
  
            // add the numbers 
            addPositiveNumbers(2, -1); 
        } 
        catch (Throwable e) { 
  
            System.out.println("Cause : "
                               + e.getCause()); 
        } 
    } 
  
    // method which adds two positive number 
    public static void addPositiveNumbers(int a, int b) 
        throws Exception 
    { 
  
        // if Numbers are Positive 
        // than add or throw Exception 
        if (a < 0 || b < 0) { 
  
            // create a Exception 
            // when Numbers are not Positive 
            // This exception will be used as a Cause 
            // of another exception 
            Exception 
                ee 
                = new Exception("Numbers are not Positive"); 
  
            // create a new Exception 
            Exception anotherEXe = new Exception(); 
  
            // initialize the cause and throw Exception 
            anotherEXe.initCause(ee); 
  
            throw anotherEXe; 
        } 
  
        else { 
  
            System.out.println(a + b); 
        } 
    } 
}
輸出:
Cause : java.lang.Exception: Numbers are not Positive

參考文獻: https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#initCause(java.lang.Throwable)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Throwable initCause() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。