📜  Java中可抛出的 initCause() 方法及示例

📅  最后修改于: 2022-05-13 01:54:52.128000             🧑  作者: Mango

Java中可抛出的 initCause() 方法及示例

Throwable 类initCause()方法用于初始化 this Throwable 的原因,并将指定的原因作为参数传递给 initCause()。实际上,原因是在发生异常时导致该可抛出对象被抛出的可抛出对象。此方法只能调用一次。通常,此方法在构造函数中调用,或在创建 throwable 后立即调用。如果调用 Throwable 是使用 Throwable(Throwable) 或 Throwable(String, Throwable) 创建的,则该方法甚至不能调用一次。

句法:

public Throwable initCause?(Throwable cause)

参数:此方法接受cause作为参数,表示 this Throwable 的原因。

返回:此方法返回对此 Throwable 实例的引用。

异常:此方法抛出:

  • IllegalArgumentException如果原因是这个可抛出的。
  • 如果此 throwable 是使用 Throwable(Throwable) 或 Throwable(String, Throwable) 创建的,或者此方法已在此 throwable 上调用,则IllegalStateException

下面的程序说明了 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: Java Java.lang.Throwable)