znq
znq

Reputation: 44973

How to get a list of all possible exceptions that could happen within a specific scope

I've a class class_A dealing with another class class_B which has methods with calls to other objects dealing with HTTP and JSON. Now I don't wanna handle these exceptions within class_B but rather on a higher level and thus forward them via throw e to class_A.

Now I'm wondering when in my class A surrounding the call to a method of class_B with try/catch how I can get all the possible Exceptions that could get forwarded from that method or methods of subclasses (like HTTP & JSON).

Preferred would be way to get the possible Exceptions directly in Eclipse, but other solutions are appreciated as well.

(Please let me know if my problem description is not clear.)


Update: What I'm looking for is not an actual implementation, but rather a list of potential exceptions, so I can see and decided for what cases I should better build a specific catch block and which Exceptions can be handled by a generic catch block.

Upvotes: 4

Views: 1437

Answers (1)

Michael Borgwardt
Michael Borgwardt

Reputation: 346566

It's impossible to get an exhaustive list of Exceptions that might be thrown, due to RuntimeExceptions that do not have to be declared.

Besides, having potentially 20 or more catch blocks for different exceptions (most likely all doing the same thing) would be insane. Instead, you simply do this:

catch(SpecialException e)
{
    // do things specific to that exception type;
    // if there are no such things, just do the
    // general catch below
}
catch(Exception e)
{
    // do generic stuff
}

Upvotes: 3

Related Questions