-4

Hi i created a method to save object in a file

public void findAndStore(String filename, double amount) { //1
        File f1 = new File (filename); //2
        FileOutputStream fos =new FileOutputStream (f1);//3
        ObjectOutputStream os =new ObjectOutputStream (fos);//4
        for (int i=0;i<nbApartment;i++){//5
            if (ar_Apartment[i].get_Electcharge()<amount){//6
                os.writeObject(ar_Apartment[i]);//7
            }
        }
        os.close ();//8
    }

it says in the line 3 error unhandled exception File not found exception , why i already created it and named it f1 .

it says in the line 4 ,7 ,8 unhandled exception type IO exception ,why .

NOTE: i already solved the problem by making the method throws exception or try and catch but my question is why there is an exception my teacher wrote it like this and he did't say anything about exceptions in this method .

  • 1
    These are checked exceptions, and functions that can cause checked exceptions need to be called in an appropriate try-catch block. Look at: http://en.wikibooks.org/wiki/Java_Programming/Checked_Exceptions – almightyGOSU Dec 09 '14 at 08:24
  • http://docs.oracle.com/javase/tutorial/essential/exceptions/ – maasg Dec 09 '14 at 08:25
  • possible duplicate of [Java: checked vs unchecked exception explanation](http://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation) – Angelo Fuchs Dec 09 '14 at 10:19

1 Answers1

0

Surround with try catch or declare the IOException in your method declaration

shikjohari
  • 2,278
  • 11
  • 23