0

I recently moved from NetBeans to Eclipse and I very much miss one great feature - whenever I use method which throws some kind of exception, NetBeans alerted me and I needed to add try-catch and NetBeans automatically generated exception type for me. Is there something similiar for Eclipse?

f.e. : Integer.parseInt(new String("foo"));

NetBeans alerts I need to catch NumberFormatException.

Eclipse doesn't alert me at all

I am using Eclipse Java EE IDE for Web Developers, 3.5 - Galileo

2 Answers 2

3

It most certainly does. You have to hit "save" (CTRL + S) before that, of course.

Of course, you shouldn't have declared the method to throw that exception (for example throws Exception)

Also make sure you have Project > Build automatically selected.

Important: You don't declare or catch RuntimeException or its subclasses - these are unchecked exceptions, and for them eclipse rightly doesn't offer any options. NumberFormatExceptions is one of these.

Try the methods of FileInputStream, for example, where IOException - a checked exception - is thrown.

(for the record - NetBeans also doesn't do anything for Integer.parseInt(..))

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

11 Comments

I use default settings and it does not. I checked editor settings and was unable to find this kind of setting.
Integer.parseInt throws a NumberFormatException, this is not manually declared...?
@Finbarr : any kind of exception, that's just an example
I was responding to @Bozho where he says "Of course, you shouldn't have declared the method to throw that exception (for example throws Exception)". This doesn't seem to relate to your question.
it does - if his method, where he calls Integer.parseInt() has declared to thrown an exception up, the error does not appear.
|
1

Eclipse relies on compiler errors to suggest try/catch exception and NumberFormatException extends RuntimeException which does not give an error if it's not surrounded by try/catch block (to avoid putting everything in blocks - think of NullPointerException which is also a RuntimeException and everything that can cause it).
Quote from javadoc: "RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.
If you try doing something like

FileInputStream f = new FileInputStream("");

which throws FileNotFoundException and does not extend RuntimeException you'll get a compiler error, and a suggestion from eclipse to use a try/catch block or to declare that the enclosing method also throws that exception.

Comments

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.