2015-07-10 93 views
0

我不断地收到错误:TestException.java:8:error:unreported exception Throwable;必须捕获或声明抛出Java中自定义异常的问题

throw new 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"); 
    } 

} 
+0

请缩进代码以便我们更有帮助 –

回答

1

initCause返回Throwable

这意味着你实际上抛出了Throwable而不是ParentException,所以编译器抱怨说你扔了Throwable但没有捕获它。

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

1

initCause()将抛出的Throwable对象,所以你应该捕获该异常

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

只是因为initCause返回一个Throwable实例:

public synchronized Throwable initCause(Throwable cause) 

虽然抓子句无法捕获任何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,如果你想赶上所有抛出异常,包括提到的三个例外和所有其他错误,异常延伸的Throwable

try { 
      throw new ParentException().initCause(new ChildException().initCause(new SQLException())); 

     } catch (Throwable e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
0

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) {} 

您可以通过多种方式解决这个问题,我觉得2种最好的方式是通过两种重载两个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)会给出错误信息,说明您试图捕获从未抛出异常。