Search code examples
javaexception

How do you handle "impossible" exceptions in Java?


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?


Solution

  • 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.