Xorty
Xorty

Reputation: 18861

Eclipse - Handling Java Exceptions like in NetBeans

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

Upvotes: 0

Views: 1706

Answers (2)

Andrei Fierbinteanu
Andrei Fierbinteanu

Reputation: 7816

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.

Upvotes: 1

Bozho
Bozho

Reputation: 597026

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

Upvotes: 3

Related Questions