views:

184

answers:

2

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

+3  A: 

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(..))

Bozho
I use default settings and it does not. I checked editor settings and was unable to find this kind of setting.
Xorty
`Integer.parseInt` throws a `NumberFormatException`, this is not manually declared...?
Finbarr
@Finbarr : any kind of exception, that's just an example
Xorty
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.
Finbarr
it does - if his method, where he calls Integer.parseInt() has declared to thrown an exception up, the error does not appear.
Bozho
@Xorty: Project > Build automatically enabled?
Bozho
@Bozho I see what you were saying now.
Finbarr
@Bozho : yes. As for exception : no, my method does not throw exception, it only uses methods already throwing (like code sample I provided)
Xorty
@Xorty see my update now..
Bozho
@Bozho: Right, it works for FileInputStream ... too bad RuntimeExceptions stays ignored :-(
Xorty
@Xorty it's ignored in netbeans (6.7.1) as well.
Bozho
Good, matter settled than. Thank you for help :)
Xorty
+1  A: 

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.

Andrei Fierbinteanu