在 Eclipse 中,框架对任何引发异常的代码强制使用 try\catch 或 throws 语句。如果不做一些异常(exception)的事情,你就无法继续。
例如,如果您写:
File propertiesFile = new File(PROPS_FILE_PATH)
Eclipse 不会构建该项目,除非您用 try/catch 包围该项目或在方法签名中添加 抛出 FileNotFoundException。
IntelliJ 不会强加这一点。
IntelliJ 中是否有并行行为?有插件吗?
各位,谢谢大家的建议。 我主要关心的是了解是否有一种方法可以实现与 Eclipse IDE 中相同的“检测和警报异常”行为。
锁定我的问题后:
如 DZone 中所述:
When creating an Exception class, the programmer has to designate a kind of exception that must be caught or optionally caught. If you declare the Exception class to extend Exception as shown below, the compiler will insist that the method that is being thrown should also be in a caught in catch block.
The compiler gives the programmer two choices when they call a method that throws an Exception that must be caught: 1. Add a try/catch in the code that is being call to catch the Exception 2. Pass the Exception back on to the caller
因此,您可以看到,Eclipse 编译器强制程序员以这种或其他方式处理异常。这也是我在 Intellij 上搜索的内容。
谢谢!
最佳答案
IntelliJ 的工作方式与 Window 的检查异常相同:它要求您处理(捕获/抛出)它。
但是 File propertiesFile = new File(PROPS_FILE_PATH)
不会抛出任何已检查的异常。看constructor javadoc .
尝试使用可能引发已检查异常的语句:
FileInputStream propertiesFile = new FileInputStream(PROPS_FILE_PATH);
My main concern was to understand if there is a way to achieve the same behavior of "detecting and alerting of exceptions" like in the Eclipse IDE.
您可以在 IDE 上的导致编译错误的语句、项目 View (当前类将以红色下划线显示)和问题 View 中找到编译错误。
IntelliJ 提供的默认意图(语句上的 alt + Enter)将在 throw 子句中添加异常或捕获异常。
在 Eclipse 中,您会在相同的位置发现编译错误,并且默认的快速修复是相同的。
所以您不应该对 IntelliJ 迷失方向。至少在这一点上。请注意,在其他方面您也可能是这样。我个人是:)
关于“警报”,IntelliJ 运行某些内容的默认方式比 Eclipse 更严格:它很快就会失败。
GhostCat 的答案很好地解决了这一点。您可能会感兴趣。
关于java - IntelliJ - 对引发异常的代码施加 try\catch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53704677/