Question

I have some code that is parsing an int from a string:

int number = Integer.parseInt(a[0]);

there is no guarantee that the string I will receive is well formed and this may throw a NumberFormatException. However, as this exception is optional, Eclipse does not give me a light bulb for generating it.

Is there a menu or context menu option to wrap an exception of a method beneath the cursor or carrot?

Was it helpful?

Solution

Highlight the block you want to enwrap, go to the top menu under Source. Choose Surround with Try / Catch Block.

It will automatically indent the code and list the found exceptions. I think the NumberFormatException will be listed automatically.

This is basically the same as with UncheckedExceptions, except that with UncheckedExceptions Eclipse will automatically highlight the line as it is a Java Error to ignore (not catch nor rethrow) a checked exception. You probably have used the QuickFix Menu (usually STRG+1 as hotkey, or mouse hover over the error). The menu item above will work even without an error (and thus no error you could use QuickFix for)

OTHER TIPS

It wont as NumberFormatException is a RuntimeException.

Imagine all the hard work eclipse would have to do to catch null pointer exceptions.Eclipse would only tell you about checked Throwables

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top