简体   繁体   English

Eclipse-处理NetBeans中的Java异常

[英]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. 我最近从NetBeans迁移到Eclipse,我非常想念一个伟大的功能-每当我使用引发某种异常的方法时,NetBeans都会提醒我,并且我需要为我添加try-catch和NetBeans自动生成的异常类型。 Is there something similiar for Eclipse? 是否有类似Eclipse的功能?

fe : Integer.parseInt(new String("foo")) ; fe: Integer.parseInt(new String("foo")) ;

NetBeans alerts I need to catch NumberFormatException. NetBeans警报,我需要捕获NumberFormatException。

Eclipse doesn't alert me at all Eclipse根本不提醒我

I am using Eclipse Java EE IDE for Web Developers, 3.5 - Galileo 我正在将Eclipse Java EE IDE用于Web开发人员3.5-Galileo

It most certainly does. 当然可以。 You have to hit "save" (CTRL + S) before that, of course. 当然,您必须先点击“保存”(CTRL + S)。

Of course, you shouldn't have declared the method to throw that exception (for example throws Exception ) 当然,您不应该声明抛出该异常的方法(例如throws Exception )。

Also make sure you have Project > Build automatically selected. 还要确保已Project > Build automatically选择“ Project > Build automatically

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. 重要提示 :您不会声明或捕获RuntimeException或其子类-这些是未经检查的异常,因此,对于eclipse而言,它们不提供任何选项。 NumberFormatExceptions is one of these. NumberFormatExceptions是其中之一。

Try the methods of FileInputStream , for example, where IOException - a checked exception - is thrown. 尝试使用FileInputStream的方法,例如, IOException一个检查到的异常。

(for the record - NetBeans also doesn't do anything for Integer.parseInt(..) ) (根据记录,NetBeans对Integer.parseInt(..)也没有任何作用)

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). Eclipse依靠编译器错误来建议try / catch异常,并且NumberFormatException扩展了RuntimeException,如果RuntimeException没有被try / catch块包围,它不会给出错误(为避免将所有内容放入块中,请考虑NullPointerException,它也是RuntimeException,并且可以原因)。
Quote from javadoc: "RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine. 引用javadoc的话:“ RuntimeException是在Java虚拟机的正常操作期间可能引发的那些异常的超类。

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. 方法不需要在其throws子句中声明RuntimeException的任何子类,这些子类在方法执行期间可能会被抛出但未被捕获。
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. 它将抛出FileNotFoundException并且不会扩展RuntimeException,您将得到一个编译器错误,以及eclipse的建议使用try / catch块或声明封闭方法也抛出该异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM