Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 574
Miquel Avatar asked Jul 02 '12 08:07

Miquel


People also ask

How do you handle exceptions in Java without try catch?

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.


1 Answers

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.

like image 118
Julien Avatar answered Sep 30 '22 07:09

Julien