unknown_boundaries
unknown_boundaries

Reputation: 1580

How to deal with methods throwing checked exceptions in java

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

What could be the proper ways to handle this situation?

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

Upvotes: 0

Views: 132

Answers (4)

ZeusNet
ZeusNet

Reputation: 720

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

Upvotes: 0

Drake29a
Drake29a

Reputation: 896

You can use:

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

But it is highly unadvised:)

Upvotes: 0

Daniel Earwicker
Daniel Earwicker

Reputation: 116664

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.

Upvotes: 1

Thihara
Thihara

Reputation: 6969

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.

Upvotes: 0

Related Questions