Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping a checked exception into an unchecked exception in Java?

I have this factory method in java:

public static Properties getConfigFactory() throws ClassNotFoundException, IOException {     if (config == null) {         InputStream in = Class.forName(PACKAGE_NAME).getResourceAsStream(CONFIG_PROP);         config = new Properties();         config.load(in);     }     return config; } 

And I want to transform the two checked exceptions into unchecked exceptions. What is the best way to go about this?

Should I just catch the exception and throw a new RuntimeException using the caught exception as the inner exception?

Is there a better way to do this or should I even be attempting to do this in the first place?

EDIT:
Just to clarify. These exceptions will be fatal, as the configuration file is essentially to the operation of the program and all exception will be caught and logged at the top level of my program.

My purpose is to avoid an unnecessary throws exception, exception added to the signature of every method that calls my factory.

like image 696
James McMahon Avatar asked Jan 27 '09 19:01

James McMahon


People also ask

How do I create a checked exception to unchecked exception?

We can create the custom unchecked exception by extending the RuntimeException in Java. Unchecked exceptions inherit from the Error class or the RuntimeException class.

Can we throw checked and unchecked exception in Java?

Yes, we can catch compile time exception (checked) and in the catch block we can wrap it with in a run time exception (unchecked) and re-throw it. But since we are re-throwing using checked exception we need to either wrap it inside an implicit try-catch pair or, skip handling it using the throws clause.

Can we handle both checked and unchecked exceptions?

A checked exception is caught at compile time whereas a runtime or unchecked exception is, as it states, at runtime. A checked exception must be handled either by re-throwing or with a try catch block, whereas an unchecked isn't required to be handled.

What is wrapping an exception?

Exception wrapping is when you catch an exception, wrap it in a different exception and throw that exception. Here is an example: try{ dao.readPerson(); } catch (SQLException sqlException) { throw new MyException("error text", sqlException); }


1 Answers

A RuntimeException should be used only when the client cannot recover from whatever the problem is. It is occasionally appropriate to do what you are talking about, but more often it is not appropriate.

If you are using a JDK >= 1.4, then you can do something like:

 try {   // Code that might throw an exception } catch (IOException e) {   throw new RuntimeException(e); } catch (ClassNotFoundException e) {   throw new RuntimeException(e); } 

and the rethrown RuntimeException will have the original cause included inside it. This way, someone at the top of the thread catching the RuntimeException -- your threads do catch RuntimeException so they don't just silently die, right? -- can at least print out the FULL stack trace of the cause.

But as others have said and will say, exceptions are checked for a reason. Only do this when you are positive that your clients cannot recover from the problem that you are rethrowing as an unchecked exception.

NOTE: Better than just RuntimeException would be to use a more specific unchecked exception if one is available. For example, if the only reason your method could throw a ClassNotFoundException is because a configuration file is missing, you could rethrow a MissingResourceException, which is an unchecked exception but gives more information about why you are throwing it. Other good RuntimeExceptions to use if they describe the problem you are rethrowing are IllegalStateException, TypeNotPresentException and UnsupportedOperationException.

Also note that it is ALWAYS a good idea for your threads to catch RuntimeException and at a minimum log it. At least this way you understand why your threads are going away.

like image 82
Eddie Avatar answered Oct 14 '22 08:10

Eddie