Why does the constructor handle the exception but I get a "not handled exception error" when I use it

3xploit

I have two constructors :

protected WordStore() {
    this.bigMap = new HashMap<>();
}

protected WordStore(String file) throws IOException {
    this.bigMap = new HashMap<>();
    String line = "";
    try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file));)
    {
        while ( (line = bufferedReader.readLine()) != null){
            System.out.print(line);
            }
    }
    catch (IOException e) {
        System.out.println("The file could not have been loaded correctly");
        }
    }

The second one handles the IOexception and it compiles correctly but when I try to initialise a new map from WordStore:

WordStore store1 = new WordStore("text.txt");

I get a compiling error saying that the IOException has not been handled. Obviously the constructors are in a different class than the store initialisation. This should be fairly easy to answer I'm just missing something.

user7

throws and catch are different. When a method declares in its signature that it throws an exception, the caller has to handle it by either rethrowing it back or by catching it.

protected WordStore(String file) throws IOException {
 //doing something that can throw an IOException
}

//Caller handles it
try {
    new WordStore("text.txt");
} catch (IOException ioex) {
    //Handle the exception
}

//Or the caller (Assuming the caller is the main method) can throw it back
public static void main(String[] args) throws IOException {
    new WordStore("text.txt");
}

Here, since you are already catching the IOException, you can remove the throws clause from the constructor.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why I get exception when I try to use AutoMapper?

Why i get this exception when i try to handle empty value on overflow menu?

How can i handle exception when i use asynctask in android?

Why do I get triple fault when trying to handle an exception on 286 but not on a modern CPU nor Bochs?

Why do I sometimes get an "Invalid window handle" exception when creating a window in a different thread

Why does illegalArgumentException get thrown when I use createBufferStrategy(3)?

Why is exit code zero ("good") when I fail to handle an exception?

Why do I get Argument exception when I try to use "Include("PropertyName")" in query EF?

Why does eclipse automatically add a java super() method in a constructor when I use the editors code generator?

Why does mocha chai should not prove identity for `return this`, if I use Proxy:get in the constructor?

Does somebody know why i get this NullReferenceException error when trying to handle a Reaction

Why does Python throw an exception when I use a decimal value that's between 0 and 1?

I get this exception when i use this script on my masterpage

Why do I get "input does not match format" when I use fmt.Fscanf?

XPages HTTP Web Server: Command Not Handled Exception Error

I get the "default is not a constructor" error when trying to use `glidejs`

Why I obtain this exception when I try to handle an AJAX request with a Laravel controller method?

Why does std::mutex throw an exception when I call lock()?

Why does this Hashmap Iteration not work? I get a NullPointer Exception

Why this exception is thrown when I try to use an iterator on this list?

Why do I get exception when checking for open Serial Port?

Why I get exception when setting ComboBox.SelectedIndex?

Why I get OutOfMemory exception when trying create a decoder

Why do I get "Duplicate entry" exception when inserting a record?

CompletableFuture does it cancel automatically when I do a get and an exception is thrown?

Why do I get an error when I use transduce?

Why do I get nil in presenter when I use MVP?

Why i get unsuspected behavior when I use the if/else statements?

Why i get Undefined variable when i use PostController in Laravel?