Why is “throws Exception” necessary when calling a function?

In Java, as you may know, exceptions can be categorized into two: One that needs the throws clause or must be handled if you don’t specify one and another one that doesn’t. Now, see the following figure: In Java, you can throw anything that extends the Throwable class. However, you don’t need to specify a … Read more

Throws or try-catch

catch an exception only if you can handle it in a meaningful way declare throwing the exception upward if it is to be handled by the consumer of the current method throw exceptions if they are caused by the input parameters (but these are more often unchecked)

Exception handling : throw, throws and Throwable

throws : Used when writing methods, to declare that the method in question throws the specified (checked) exception. As opposed to checked exceptions, runtime exceptions (NullPointerExceptions etc) may be thrown without having the method declare throws NullPointerException. throw: Instruction to actually throw the exception. (Or more specifically, the Throwable). The throw keyword is followed by … Read more

Error message “unreported exception java.io.IOException; must be caught or declared to be thrown” [duplicate]

void showfile() throws java.io.IOException <—– Your showfile() method throws IOException, so whenever you use it you have to either catch that exception or again thorw it. Something like: try { showfile(); } catch(IOException e) { e.printStackTrace(); } You should learn about exceptions in Java.