Java中面临自定义异常问题

Java中面临自定义异常问题,java,exception,Java,Exception,我不断地得到错误:TestException.java:8:error:unreported exception Throwable;必须被抓住或宣布被抛出 抛出新的ParentException().initCause(新的 ChildException().initCause(新的SQLException()) 不管怎么说,我知道有些琐碎的东西不见了,我想不起来了,谢谢,不要从表面上看问题,我只是想更新我的理解 import java.sql.SQLException; p

我不断地得到错误:TestException.java:8:error:unreported exception Throwable;必须被抓住或宣布被抛出

抛出新的ParentException().initCause(新的 ChildException().initCause(新的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");
    }

}

initCause
返回可丢弃的

try {
    throw new ParentException().initCause(new ChildException().initCause(new SQLException()));
} catch (Throwable e) {
     e.printStackTrace();
     try {
         Thread.sleep(10*1000);
     } catch(Exception ex) {}
这意味着您实际上是在抛出一个
Throwable
而不是
ParentException
,因此编译器抱怨您抛出了一个
Throwable
,但没有捕获它


您可以通过更改
ParentException
ChildException
来解决此问题,这样他们不仅可以接收错误消息,还可以接收原因。然后可以调用

initCause
返回一个可丢弃的

try {
    throw new ParentException().initCause(new ChildException().initCause(new SQLException()));
} catch (Throwable e) {
     e.printStackTrace();
     try {
         Thread.sleep(10*1000);
     } catch(Exception ex) {}
这意味着您实际上是在抛出一个
Throwable
而不是
ParentException
,因此编译器抱怨您抛出了一个
Throwable
,但没有捕获它


您可以通过更改
ParentException
ChildException
来解决此问题,这样他们不仅可以接收错误消息,还可以接收原因。然后可以调用

initCause
返回一个可丢弃的

try {
    throw new ParentException().initCause(new ChildException().initCause(new SQLException()));
} catch (Throwable e) {
     e.printStackTrace();
     try {
         Thread.sleep(10*1000);
     } catch(Exception ex) {}
这意味着您实际上是在抛出一个
Throwable
而不是
ParentException
,因此编译器抱怨您抛出了一个
Throwable
,但没有捕获它


您可以通过更改
ParentException
ChildException
来解决此问题,这样他们不仅可以接收错误消息,还可以接收原因。然后可以调用

initCause
返回一个可丢弃的

try {
    throw new ParentException().initCause(new ChildException().initCause(new SQLException()));
} catch (Throwable e) {
     e.printStackTrace();
     try {
         Thread.sleep(10*1000);
     } catch(Exception ex) {}
这意味着您实际上是在抛出一个
Throwable
而不是
ParentException
,因此编译器抱怨您抛出了一个
Throwable
,但没有捕获它

您可以通过更改
ParentException
ChildException
来解决此问题,这样他们不仅可以接收错误消息,还可以接收原因。然后可以调用initCause()将抛出Throwable对象,因此应该捕获该异常

    catch (Throwable e)
    {            
        e.printStackTrace();
    }
initCause()将抛出Throwable对象,因此您应该捕获该异常

    catch (Throwable e)
    {            
        e.printStackTrace();
    }
initCause()将抛出Throwable对象,因此您应该捕获该异常

    catch (Throwable e)
    {            
        e.printStackTrace();
    }
initCause()将抛出Throwable对象,因此您应该捕获该异常

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

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

public synchronized Throwable initCause(Throwable cause)
While catch子句无法捕获任何可丢弃的
引用。您可以添加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,包括前面提到的三个异常和所有其他错误,扩展Throwable的异常

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

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

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

public synchronized Throwable initCause(Throwable cause)
While catch子句无法捕获任何可丢弃的
引用。您可以添加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,包括前面提到的三个异常和所有其他错误,扩展Throwable的异常

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

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

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

public synchronized Throwable initCause(Throwable cause)
While catch子句无法捕获任何可丢弃的
引用。您可以添加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,包括前面提到的三个异常和所有其他错误,扩展Throwable的异常

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

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

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

public synchronized Throwable initCause(Throwable cause)
While catch子句无法捕获任何可丢弃的
引用。您可以添加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,包括前面提到的三个异常和所有其他错误,扩展Throwable的异常

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

        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
initCause(Throwable-cause)
函数返回一个对象
Throwable
:当您执行
throw new ParentException().initCause(new ChildException().initCause(new SQLException()),您基本上是在执行
抛出(new ParentException().initCause(new ChildException().initCause(new SQLException()))
,因此您正在执行
throw
,因此必须捕获
Throwable
的异常

try {
    throw new ParentException().initCause(new ChildException().initCause(new SQLException()));
} catch (Throwable e) {
     e.printStackTrace();
     try {
         Thread.sleep(10*1000);
     } catch(Exception ex) {}
您可以通过多种方式解决此问题,我认为最好的两种方式是通过在
ParentException
ChildException
中使用

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
正文中可能引发的唯一异常。试图捕获其他异常(不是父异常的超类)将导致错误,即您试图捕获从未抛出的异常。

initCause(Throwable cause)
函数返回一个对象
Throwable
:当您执行
throw new ParentException().initCause时也是如此(new ChildException().initCause(new SQLException());
,您基本上是在执行
抛出(new ParentException().initCause(new ChildException().initCause(new SQLException()));
,因此您必须捕获
抛出的异常

try {
    throw new ParentException().initCause(new ChildException().initCause(new SQLException()));
} catch (Throwable e) {
     e.printStackTrace();
     try {
         Thread.sleep(10*1000);
     } catch(Exception ex) {}
您可以通过多种方式解决此问题,其中有两种最佳方式