2

In the following piece of code :

    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();
    }
}

I'm trying to catch a checked exception and handle it. In my case it would be FileNotFoundException, but I don't know for what reason it never runs the catch block which I'm trying to print a message or any other functionality, but it seems it jumps over.Thank you

Farhad
  • 12,178
  • 5
  • 32
  • 60
shirin
  • 152
  • 1
  • 14
  • 1
    Well, if the `catch` is not reached, it is simply that you didn't get any FileNotFoundException. Are you sure there is no `"/Project1/src/employee.txt"` file ? – AxelH Jan 27 '17 at 13:02
  • What is the exact output? And did you try running in a debugger? – GhostCat Jan 27 '17 at 13:03
  • The whole point of this code is to learn how to handle a checked exception. For that reason there is no "/Project1/src/employee.txt" so it is throws a checked exeption – shirin Jan 27 '17 at 13:05
  • it prints: This block always executes. Process finished with exit code 0 //which means it goes to finally block directly – shirin Jan 27 '17 at 13:08
  • 4
    I executed your code (without the file `/Project1/src/employee.txt`) and it prints the two expected sentences `An IO exception happened and has been handled` and `This block always executes.` If the first sentence is not printed, that means you have the file `/Project1/src/employee.txt` on your computer. By the way you could write directly `BufferedReader br = new BufferedReader(new FileReader("/Project1/src/employee.txt"));` – Olivier Boissé Jan 27 '17 at 13:11
  • I have ran Your code, and the output is as expected. It reaches `FileNotFoundException` and prints `An IO exception happened and has been handled`. You can also add `throws FileNotFoundException` at the end of `public void throwCheckedException()` method, and move `try/catch` block to Your main method. The output will be the same, and You can check if Your output is as expected. – drstonecodez Jan 27 '17 at 13:15
  • @oliv37 Thank you for your comment! I think you found my answer. I use to think project can only use file in the same file. The text file physically exist in "/Project1/src/employee.txt", but it's not the project I'm working on. That's why I couldn't catch it. Thank you :) – shirin Jan 27 '17 at 13:19
  • 2
    Replace `/Project1/src/employee.txt` with `garbage-ksjhdlk.txt`, and give it a try. – Sergey Kalinichenko Jan 27 '17 at 13:24

0 Answers0