Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse: find lines in a block that can throw exceptions

I'm looking at code that looks like

try {
     // Lots of things here.
     // More than I'd like to individually hover over every time I see this
} 
catch (Exception e) {
     // doesn't matter
}

For any particular method in the try block, I can find what checked exceptions it throws. Is there any way to highlight all lines that could throw some checked exception?

In general, I guess I could perhaps remove the catch block, changing the method signature to throw Exception, at which point, I can see all lines in the method that throw an exception (Nevermind: see the update).

In this case, that won't even work nicely, because the code is in a JSP.

Note: if it matters, I'm using MyEclipse standard.

Update: Mark occurrences is on, but simply does nothing in this case. I asked the question because I thought that something about the context made this expected behavior, but it looks like it's a weird edge case or bug.

like image 537
Justin Blank Avatar asked Oct 21 '22 13:10

Justin Blank


2 Answers

In eclipse, if you enable "highlight occurrences" and select Exception, it would highlight all lines that are throwing a (checked) exception that is being caught by the catch block.

like image 96
Kshitij Avatar answered Oct 27 '22 09:10

Kshitij


Place you cursor in the middle of the word "Exception" (or whatever type of exception you catch) in the catch clause. The word "Exception" and all method calls throwing such an exception will automatically be marked with a gray background highlighting. This feature is called "Mark occurrences" in Eclipse.

If you do not see this happen in your Eclipse installation, hit AltShiftO (O like Occurances) once to toggle the feature and repeat above workflow. Or use the toobar button (second entry in this list of toolbar buttons).

like image 45
Bananeweizen Avatar answered Oct 27 '22 08:10

Bananeweizen