1

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?

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

2 Comments

The missing piece was that I needed to highlight the entire expression, not just the method. This is perfect, thanks!
Well, you can wrap multiple lines if you want. But highlighting less could cause Eclipse to missunderstand you. If it's just a part of the line, it could either decide to widen the selection to the whole line or to disable the menu item - which wouldn't be what you want. Glad I could help.
1

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

1 Comment

My question is about how to generate code for an exception not to have Eclipse catch it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.