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.