简体   繁体   中英

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.

在eclipse中,如果启用“突出显示”并选择“ Exception ,它将突出显示正在抛出由catch块捕获的(已检查)异常的所有行。

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 Alt Shift O (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 ).

You can read the class' reference manual to figure out what exception a particular method can throw. Keep in mind java has two kinds of exception: checked and unchecked. With unchecked exception, the method doesn't have to declare it can throw the exception (for example RuntimeException)

I'd also recommend you use some IDE such as Eclipse, it will instantly tell you if a line can throw exception and you're not handling it -- you can then wrap it with try-catch or have the method throw it just by single mouse click.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM