I have a snippet as follows:
try
{
//blah!!
}
catch(IOException e)
{
throw new RuntimeException(e);
}
I do not understand how the above works? Will it catch an IOException and when it does that will it throw a RuntimeException? In that case the IOException will not have any meaning right? Some example would help.
You’re right about some assumptions, but you should go a bit deeper.
Inside your
tryblock, suppose one of your methods of your class throws this kind of exceptionIOException. So catch will work as way for you to treat this exceptional case. It’s basically this. If you just throw your exception away using a RuntimeException, but as you did, wrapping yourIOExceptioninRuntimeExceptionyou won’t lose it at all.A normal use is in a higher level treat your exception. Here’s a good tutorial about Exception Handling, please check: Best Practices for Exception Handling