【问题标题】:Facing Issue with Custom Exception in Java面对 Java 中的自定义异常问题
【发布时间】:2015-07-10 17:31:37
【问题描述】:

我不断收到错误:TestException.java:8: error: unreported exception Throwable;必须被抓住或宣布被扔掉

抛出新的 ParentException().initCause(new ChildException().initCause(new SQLException()));

任何想法,我知道缺少一些琐碎的事情,我并没有很容易想到,谢谢,不要以面值作为来源,我只是想刷新我的理解。

    import java.sql.SQLException;

    public class TestException{

    public static void main(String[] args) {

    try{
            throw new ParentException().initCause(new ChildException().initCause(new SQLException()));
    }
    catch(ParentException | ChildException | SQLException e){
            e.printStackTrace();
            try{
                    Thread.sleep(10*1000);
            }
            catch(Exception et){
                    ;
            }
    }

    }
 }

  class ParentException extends Exception{

    public ParentException(){
            super("Parent Exception is called");
    }

}

 class ChildException extends Exception{

    public ChildException(){
            super("Child Exception is called");
    }

}

【问题讨论】:

  • 请更好地缩进您的代码,以便我们提供更多帮助

标签: java exception


【解决方案1】:

initCause 返回一个Throwable

这意味着您实际上是在抛出 Throwable 而不是 ParentException,因此编译器会抱怨您在抛出 Throwable 但没有捕捉到它。

您可以通过更改 ParentExceptionChildException 来解决此问题,这样它们不仅会收到错误消息,还会收到原因。然后您可以拨打Exception constructor that receives a message and a cause

【讨论】:

    【解决方案2】:

    initCause() 将抛出 Throwable 对象,因此您应该捕获该异常

        catch (Throwable e)
        {            
            e.printStackTrace();
        }
    

    【讨论】:

      【解决方案3】:

      仅仅因为initCause 返回一个Throwable 实例:

      public synchronized Throwable initCause(Throwable cause)
      

      虽然 catch 子句无法捕获任何 Throwable 引用。您可以添加 Throwable 以及其他异常。

      try {
                  throw new ParentException().initCause(new ChildException().initCause(new SQLException()));
              } catch (ParentException | ChildException | SQLException e) {
                  e.printStackTrace();
                  try {
                      Thread.sleep(10 * 1000);
                  } catch (Exception et) {
                      ;
                  }
              } catch (Throwable e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
      

      或者只要捕获 Throwable ,如果你想捕获所有 throwables,包括提到的三个异常和所有其他错误,扩展 Throwable 的异常

      try {
                  throw new ParentException().initCause(new ChildException().initCause(new SQLException()));
      
              } catch (Throwable e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
      

      【讨论】:

        【解决方案4】:

        initCause(Throwable cause) 函数返回Throwable 的对象:public synchronized Throwable initCause(Throwable cause),所以当您执行throw new ParentException().initCause(new ChildException().initCause(new SQLException())); 时,您基本上是在执行throw ( new ParentException().initCause(new ChildException().initCause(new SQLException())) ) ;,所以您正在执行throw <ThrowableObject>,因此您必须捕获一个异常Throwable.

        try {
            throw new ParentException().initCause(new ChildException().initCause(new SQLException()));
        } catch (Throwable e) {
             e.printStackTrace();
             try {
                 Thread.sleep(10*1000);
             } catch(Exception ex) {}
        

        您可以通过多种方式解决此问题,我认为最好的两种方式是使用

        重载 ParentExceptionChildException 中的构造函数
        public ParentException(Throwable cause) { 
            super("Parent Exception is called", cause);
        }
        

        ChildException 和使用的类似版本

        throw new ParentException(new ChildException(new SQLException()));
        

        代替initcause(),改为执行以下操作

        try {
            ParentException p = new ParentException();
            p.initCause(new ChildException().initCause(new SQLException()));
            throw p;
        } catch (ParentException e) {
            e.printStackTrace();
            try{
                Thread.sleep(10*1000);
            } catch(Exception ex) {}
        

        注意:您只需要捕获ParentException 而不需要其他异常,因为它是唯一可能在try 的主体内抛出的异常。尝试捕获其他异常(不是ParentException 的超类将给出您试图捕获从未抛出的异常的错误。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-27
          • 1970-01-01
          相关资源
          最近更新 更多