0

I'm using a API which is throwing checked exception in mostly methods, even a constructor is throwing a checked exception.

When using API methods I want to deal it with try catch all the time. Suppose I'm using some methods 10 times then I need to surround it in try catch block 10 times.

Thing is

  • I don't want to propagate it to upper layer.
  • I want to get rid of try catch block all the time I use those methods.

What could be the proper ways to handle this situation?

Someone told me about callable interface. How can we use it in this case?

4
  • if you are unable to modify the API then it sounds like you'd need to write a wrapper method for each of the API methods that you want to use. The wrapper could then perform the exception handling and each place where you would usually reference the API directly, you would reference your wrapper method. Commented Oct 8, 2013 at 8:39
  • but if there are 50 methods then I need to write 50 wrapper methods in this case. Is there any better way. Someone told me about callable interface. How can we use it in this case? Commented Oct 8, 2013 at 9:09
  • Is it a well-known API, like JDBC? There might already be a wrapper (like Spring's JDBCTemplate) Commented Oct 8, 2013 at 9:23
  • 1
    I've note heard of callable interfaces being used to solve a problem such as this so sorry can't comment. I agree it could be time consuming if there are 50+ methods, but it's a one off job if done correctly. My advice would be to speak to other users of this same API and understand how they have solved the same problem, if at all. Commented Oct 8, 2013 at 9:44

4 Answers 4

1

You could write classes that act as a thin wrapper around the API's own classes.

But that generally doesn't make sense. Don't you want your own code to behave differently when there is an exception? It's usually important information telling you that what ever you asked to happen is not going to happen.

Sign up to request clarification or add additional context in comments.

3 Comments

You can use: Thread.setDefaultUncaughtExceptionHandler( new DefaultExceptionHandler(...));
@Daniel, yes I do want. But lots of try-catch blocks are cluttering the code.
@user1106018 - that doesn't stop the compiler from telling you to either catch a checked exception or re-declare it as thrown by your own method.
0

If you are certain that you don't need those exceptions to be checked and handled separately you can write wrappers to handle the exceptions and delegate your method call accordingly.

Comments

0

You can use:

Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler(...));

But it is highly unadvised:)

2 Comments

no I don't want to do this. I do want to catch exception but not all the time.
You can define inside what exceptions you want to catch, let's say you will catch only NullPointerException
0

If you use these methods in one of your own methods you could surround your method body with one try-catch block which only catchs the specified checked exception.

Hope it helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.