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.

Should I declare unchecked exceptions in the throws specification?

If for some reason I can reasonably expect an unchecked exception to occur in a method, should I add it to the throws specification? Since unchecked exceptions indicate programming errors, declaring them in the throws clause should be avoided. Generally, catching these exceptions should not be attempted, except for the highest level of your program. … Read more

Either re-interrupt this method or rethrow the “InterruptedException issue in sonar

To “re-interrupt” as a best practice: try{ //some code } catch (InterruptedException ie) { logger.error(“InterruptedException: “, ie); Thread.currentThread().interrupt(); } catch (ExecutionException ee) { logger.error(“ExecutionException: “,ee); } Usually, when a thread is interrupted, whoever is interrupting the thread, wants the thread to exit what it’s currently doing. However, make sure that you do NOT multi-catch: catch … Read more

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

Is there a way to make Runnable’s run() throw an exception?

You can use a Callable instead, submitting it to an ExecutorService and waiting for result with FutureTask.isDone() returned by the ExecutorService.submit(). When isDone() returns true you call FutureTask.get(). Now, if your Callable has thrown an Exception then FutureTask.get() wiill throw an Exception too and the original Exception you will be able to access using Exception.getCause().

error code: 521