Java:Throwable编译错误must be caught or declared to be thrown

下面的语句中,如果throw t1 换成 throw t 就会有编译错误,说是must be caught or declared to be thrown,为啥?

    try{
        FileInputStream fis = new FileInputStream(file);
        Throwable t = null;
        try{
            fis.read();
        }catch (Throwable t1){
            t = t1;
            throw t1;
        } finally{
            ...
        }
    } catch(Exception e){
        ...
    }
阅读 5.1k
1 个回答

除了RuntimeException及其子类,其他异常都是要处理的
我奇怪的是难道你throw t1就能编译通过了?

编辑:
最外层catch的是Exception,t是Throwable,是Exception的父类对象,捕获不到,可以把最外层Exception改成Throwable看看
而t1实际上是IOException所以可以捕获

推荐问题
宣传栏