-1

I am using FileReader wrapped in BufferedReader to read from 2 files, in case of an exception being thrown I want to know which file caused the error.

Is there a way of finding that out?

try (BufferedReader input1 = new BufferedReader(new FileReader(pathToFile1));
     BufferedReader input2 = new BufferedReader(new FileReader(pathToFile2))){

// some more code here 

} catch (FileNotFoundException e) {
            System.err.println("File Not Found");
            System.err.println(e.getMessage());
            System.exit(2);
} catch (IOException e) {
            System.err.println(e.getMessage());
            System.exit(1);

Basically I want to know which file was not found and caused the first exception to be thrown.

  • What's the problem with adding another one `try` / `catch`? – Martin Zeitler May 15 '22 at 20:12
  • 2
    Exception message should contain information about location of file which couldn't be found. Isn't that enough? OR do you want to know which *variable* was not created? What is your goal? (for now your question looks like [XY problem](https://meta.stackexchange.com/q/66377)) – Pshemo May 15 '22 at 20:12
  • 1
    If all else fails, you can always make your own `Reader` implementation that contains a `BufferedReader` but throws a subclass of `IOException` that has the information you want. But I'd be amazed if there isn't a way to figure this out from `IOException` already; I just don't readily know how. – Silvio Mayolo May 15 '22 at 20:14
  • Nothing wrong with another ```try / catch ```, I am just curious, it makes since to know which source caused the problem, for better error handling, or it might not, I am still a student and learning. – Lucas Larsson May 15 '22 at 20:15
  • Edited for a more clear goal, @Pshemo – Lucas Larsson May 15 '22 at 20:18
  • 1
    "I want to know which file was not found and caused the first exception to be thrown" `e.getMessage()` should already generate message like `foo/bar/my_file.txt (was not found)`. What is the message you are getting instead? – Pshemo May 15 '22 at 20:31
  • 1
    BTW if you use `e.printStackTrace()` you will also get line number of place from which exception was thrown. If your goal was to make debugging easier then this info would be very helpful. Also take a look at [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/q/3988788) – Pshemo May 15 '22 at 21:08

1 Answers1

0

Though I wouldn't recommend it (as it depends on linenumbers which might change if run through a preprocessor) You could use something like this

    int topLine= new Throwable().getStackTrace()[0].getLineNumber(); 
    try (BufferedReader input1 = new BufferedReader(new FileReader("pop"));
         BufferedReader input2 = new BufferedReader(new FileReader("poo"))){

        // some more code here 

        } catch (Exception e) {
                StackTraceElement[] er= e.getStackTrace();
                
                
                int exceptLine = er[er.length-1].getLineNumber() - topLine;
                
                if (exceptLine==1) {
                    // error in first buffered reader
                } else if (exceptLine==2) {
                    // error in second buffered reader
                } else {
                    // error elsewhere
                }
        }
Tony Weston
  • 317
  • 2
  • 9