Java: throw an exception rethrow: Unhandled exception type Exception

I would like to catch an exception, register it, set a flag and repeat the same exception

I have this code:

public Boolean doJobWithResult() { boolean result = true; final Feed feed = Feed.findById(feedId); try { feed.fetchContents(); } catch (Exception ex) { result = false; Logger.info("fetching feed(%d) failed", feedId); throw ex; } return result; } 

But eclipse complains about throw ex, saying that it is "Unhandled exception type Exception", and offers me to add a try-catch block around it.

Actually, I want the process to call this method to handle the exception, and not handle it myself ... I just want to return true if everything is ok, and write it down if there is an exception

On the other hand, I can wrap an exception in another exception, but I cannot throw it.

any idea?

+7
source share
9 answers

I think there are different things here:

  • You either want doJobWithResult() return true on success and false on error, or return nothing on success, and throw an exception on error. Both are simultaneously impossible. In the first case, catch an Exception, register it and return false, in the second case, change your signature to return void , and throw an exception and handle it in the caller.
  • This does not catch the exception, log it and reconstruct it. What for? Since the potential caller of your method does not know that you are already registering it, and also register it. Either throw an exception (in this case, the caller must deal with it), or catch it and process it (write it down).
  • Note that throwing an Exception does not give the caller of your method any hint about what might potentially be wrong in your method, it is always better to throw more specific exceptions or wrap the exception in a user-defined one and restore it.
  • Moreover, if you throw an Exception , the caller may be tempted to catch an Exception without noticing that it will also catch every RuntimeException (as it is derived from Exception ), which may be an undesirable behavior.
+2
source

Your doJobWithResult method should declare that it can throw an exception:

 public Boolean doJobWithResult() { 

becomes

 public Boolean doJobWithResult() throws Exception { 
+7
source

You can throw the same exception by adding throws Exception to your method signature. Otherwise, you may throw a RuntimeException .

 public Boolean doJobWithResult() { boolean result = true; final Feed feed = Feed.findById(feedId); try { feed.fetchContents(); } catch (Exception ex) { result = false; Logger.info("fetching feed(%d) failed", feedId); throw new RuntimeException(ex); } return result; } 

In this case, you do not need to indicate that the public Boolean doJobWithResult() is throwing something, but make sure you handle it correctly later (catch or expect your thread to stop ... this is a RuntimeException after all).

+4
source

If doJobWithResult should not handle the exception, remove the catch block and add a "throws Exception" to the method signature. Exception logging can be done in the class / method that should deal with the Exception in the corresponding try / catch block.

+3
source

Since Exception checked , an alternative to catching Exception is to declare your method as a metal throw:

 public Boolean doJobWithResult() throws Exception { // ... } 
+2
source

There is no need to set the result as false in the catch block, since the value will not be returned (since we throw an exception).

Your method should also indicate that it throws an exception, so the client will be forced to handle it.

Also consider using the more specific exception that will be thrown in this particular case.

+2
source

Add throws Exception to your method. You also do not need to add result = false; into the catch .

+1
source

I think that a way to eliminate this exception is really suitable if any failure of the feed.fetchContents () method cannot be repaired. (The idea is better to stop rather than continue) In addition, I suggest you use a more specific hierarchy of exceptions.

And one more thing that I got from an effective java book is that you write a method that should document using @throw (in the comments) with the reason.

+1
source

You can throw an exception thrown

  Logger.info("fetching feed(%d) failed", feedId); throw new RuntimeException(ex); 
0
source

All Articles