0

See code sample below Error message:

Error:(79, 22) java: unreported exception java.io.IOException; must be caught or declared to be thrown

Why do i get this? How can i fix it?

 public AnimalStats() throws IOException{
    simulator = new Simulator();
    try{
        fos = new FileOutputStream("AnimalStats.csv",true);
        pw = new PrintWriter(fos);
    }
    catch(IOException e) {
        System.out.println("Error has been caught!");
        e.printStackTrace();

    }
}
pjs
  • 18,696
  • 4
  • 27
  • 56
  • 4
    Remove `throws IOException` from the method. You're already catching that exception – QBrute Feb 24 '19 at 13:59
  • And you need a return type for AnimalStats. – vs97 Feb 24 '19 at 14:02
  • Please check [this](https://stackoverflow.com/search?q=unreported+exception+java.io.IOException%3B+must+be+caught+or+declared+to+be+thrown). – Ahmad F Feb 24 '19 at 14:25
  • The code that you've posted doesn't produce the error message that you claim to be getting. Please [edit] your question to show all of the relevant code. – Kenster Feb 24 '19 at 17:34

2 Answers2

0

When you add throws Exception to the method signature, that requires that the exception is handled 'upstream' at the point where the method is called.

Something like this:

    try{
     AnimalStats();

}catch(IOException ex){
     // DO SOMETHING
    }

However, If you leave the signature silent on this point, you can handle the exception within the method with your try/catch blocks, as you have done. But for that, you need to remove throws from method signature. Like this:

public AnimalStats(){
    simulator = new Simulator();
    try{
        fos = new FileOutputStream("AnimalStats.csv",true);
        pw = new PrintWriter(fos);
    }
    catch(IOException e) {
        System.out.println("Error has been caught!");
        e.printStackTrace();

    }
}

You can use either of the approach.

-1

When you specify the method "throws (an) Exception" the compiler expects an exception to occur, so he can "throw" it back to the caller. But when you handle the Exception using the "try/catch" blocks, no exception can be "thrown" back to the caller of the method (because the exception has been handled already).

A "correct" version of you code would be either -

public AnimalStats(){
simulator = new Simulator();
try{
    fos = new FileOutputStream("AnimalStats.csv",true);
    pw = new PrintWriter(fos);
}
catch(IOException e) {
    System.out.println("Error has been caught!");
    e.printStackTrace();
}
}

or

public AnimalStats() throws IOException{
simulator = new Simulator();
fos = new FileOutputStream("AnimalStats.csv",true);
pw = new PrintWriter(fos);
}

There is a major difference though!

In the first approach the method handles the Exception within itself. It "doesn't want" an exception to occur and be returned to the function who called AnimalStats().

As opposed to the first approach, in the latter we declare the method throws (an) IOException. We do not intend to handle the exception within the method and we "throw" the exception back to the function who called AnimalStats() and let them handle it.

ElayM
  • 66
  • 1
  • 7
  • This isn't a very good answer. You can declare a method to throw an exception when it doesn't actually throw it. – Kenster Feb 24 '19 at 17:35
  • That's correct, but that doesn't deny the fact that if an exception is to occur, it isn't handled inside the method but thrown back to the caller. Anyway, I did my best explaining this (after all its my first answer on stackoverflow :3). – ElayM Feb 25 '19 at 12:33