Stephen
Stephen

Reputation: 19944

Is it possible to find where a specific thrown exception could be caught in eclipse?

I'm trying to trace a IO resource leak, where a connection is opened, but not necessarily closed:

try{ 
     ...
     urlConnection.connect();
     ...
     connectionResult.setResultInputStream(urlConnection.getInputStream());
     return connectionResult;

} catch (IOException e) {
     throw new ValidationException(e, new LocationData(submissionURL.toExternalForm(), -1, -1));
}

Is there a way I can find out where ValidationException will be caught through the call hierarchy? The manual steps are something like:

  1. Do call heirarchy of containing method
  2. For each caller:
    • Analyse surrounding code, finding try catch blocks
    • If exception caught, evaluate stream closing
    • If not, repeat 1.

Notes

Upvotes: 7

Views: 3283

Answers (5)

weberjn
weberjn

Reputation: 1985

Bug 296947 - [call hierarchy] Find where catch blocks are for an exception https://bugs.eclipse.org/bugs/show_bug.cgi?id=296947

Upvotes: 0

Stephen C
Stephen C

Reputation: 718788

This is certainly not supported by Eclipse "out of the box". Their might be an Eclipse plugin that supports this, but I've not heard of one. (This is something that you rarely need to do in practice ... so there's little justification for going to the effort of implementing this.)

One alternative to code analysis is to hack the code to throw the exception under some circumstance that you can control, and then use the Java debugger to see where it actually gets caught.

Another alternative might be to hack together a custom PMD rule to identify the relevant catches. I don't think it would be simple though ...

Upvotes: 1

karmakaze
karmakaze

Reputation: 36134

You can use a memory analyzer, such as jvisualvm/visualvm and have it log object allocations. When you expect all the resources to be closed, check the live objects and find out where they were constructed.

Upvotes: 0

Paul Webster
Paul Webster

Reputation: 10654

What about dynamic analysis? If you can debug your program, you can set a Breakpoint Exception from the Breakpoints view. It's the little button with an '!' and a 'J' on it, and it will break any time the exception you care about is thrown. Then you can step to see where it is caught.

Of course it'll only find those cases that you can actual exercise by debugging.

Upvotes: 0

Prakash G. R.
Prakash G. R.

Reputation: 4892

Go to Search->Java. In the dialog you can search for Constructors and set the Limit to References. This should give you all the places where the Exception is being thrown.

Upvotes: 0

Related Questions