Sometimes, I'll end up having to catch an exception that I know can never happen, such as here
URLDecoder.decode("some string", "UTF-8"); //No unknown encoding possible
or here:
public void methodWithURL(URL url){
URI uri = new URI(url); //No invalud URI syntax possible
}
How do you handle this? I generally log a funny error regarding how the laws of the universe have changed, and then throw a RuntimeException
. Is there a better way?
throws: The throws keyword is used for exception handling without try & catch block. It specifies the exceptions that a method can throw to the caller and does not handle itself.
I catch the Exception
and wrap it in an Error
. from the doc of Error
:
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With