Reputation: 7844
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.
Upvotes: 3
Views: 6152
Reputation: 14634
You're right about some assumptions, but you should go a bit deeper.
Inside your try
block, suppose one of your methods of your class throws this kind of exception IOException
. 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 your IOException
in RuntimeException
you 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
Upvotes: 2
Reputation: 68907
No. There is something special to RuntimeException. When you want to throw a RuntimeException, you don't need a throws RuntimeException
in the method signature. This is called an unchecked exception. This code wraps the IOException into a RuntimeException and rethrows it to the caller, which is IMHO a bad approach, in most cases.
All subclasses of RuntimeException are unchecked, like IllegalArgumentException, NullPointerException, etc...
Upvotes: 1
Reputation: 30042
Checked Exceptions
have to be caught or declared as thrown. RuntimeExceptions
do not, so catching and rethrowing as a RuntimeException
is way to avoid having to declare:
public void myMethod() throws IOException
When throw new RuntimeException(e);
is called a new exception is created an thrown, but the original exception is wrapped inside it. So the stack trace will look like this:
Exception in thread "main" java.lang.RuntimeException: java.io.IOException: Some error.
at com...main(SomeClass.java:36)
Caused by: java.io.IOException: Some error.
So the actual exception that goes up the call stack was a RuntimeException
but the original IOException
is kept in the message as the root cause.
Upvotes: 1
Reputation: 373462
Your understanding is correct. If an IOException
is thrown, the catch
handler will catch it, then immediately throw its own RuntimeException
. This exception might be caught elsewhere in the program, in which case control will pick up at the handler, or it will be uncaught and will terminate the current thread.
One way to think about what's going on here is the following - an IOException
is checked exception, which means that it must be caught. If it's not caught, then the program won't compile. The above code says that whenever it catches an IOException
, it will throw a RuntimeException
, which is an unchecked exception. This exception doesn't have to be caught if the programmer doesn't want to catch it. Notice that this RuntimeException
is constructed with the caught IOException
as a parameter. This means that if the RuntimeException
is later caught, whoever catches it can notice that the underlying reason was an IOException
and can handle it accordingly.
Hope this helps!
Upvotes: 1
Reputation: 66677
Yes your understanding is correct. This may be useful in cases where you want to display application specific exceptions instead of java provided exceptions (IOException may not be perfect case to understand this).
Upvotes: 1
Reputation: 159874
In this code the checked IOException becomes effectively unchecked. By passing the IOException into the RuntimeException you are chaining the 2 together.
Upvotes: 1