检查java中的异常处理

时间:2017-01-27 13:00:46

标签: java exception-handling checked checked-exceptions

在下面的代码中:

    import java.io.*;
    import java.io.FileReader;

public class ExceptionPropagationDemo {

    public static void main(String[] args){
        ExceptionPropagationDemo testObject =new ExceptionPropagationDemo();
        testObject.throwException1(); 
    }

    public void throwCheckedException(){
        try{ //try - catch error block
            BufferedReader br;
            br = new BufferedReader(new FileReader("/Project1/src/employee.txt"));
        } catch(FileNotFoundException e){
            System.out.println("An IO exception happened and has been handled");
        }
        finally {
            System.out.println("This block always executes.");
        }
    }

    public void throwException2() {
        throwCheckedException();
    }

    public void throwException1(){
        throwException2();
    }
}

我试图捕获已检查的异常并处理它。在我的情况下,它将是FileNotFoundException,但我不知道它为什么从不运行我尝试打印消息或任何其他功能的catch块,但它似乎跳过了。谢谢你

0 个答案:

没有答案