- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个我似乎无法理解的类调试任务。我们被告知要调试作业,以便作业的输出看起来像这样。
输出:
There is a problem with the Eagle!
Java Result: 9999
给出的代码如下:
//Superclass for the custom exception.
public class EagleLandingException extends Exception
{
public EagleLandingException(String msg)
{
super(msg);
}
}
和主类
public class ThrowEagleExceptionTest {
public static void main(String[] args) {
try {
EagleLanding();
}
catch (EagleLandingException badEagle) {
System.out.printf("%s\n", badEagle.getMessage());
System.exit(9999);
}
}
private static void EagleLanding () {
EagleLandingException("There is a problem with the Eagle!");
System.exit(9999);
}
我在行中收到“永远不会抛出异常”的错误
catch (EagleLandingException badEagle)
同时在线上收到“找不到符号”错误
EagleLandingException("There is a problem with the Eagle!");
我不明白为什么会这样,并且查看了已经发布的其他问题,但似乎无法弄清楚问题是什么。先感谢您。
最佳答案
问题就在这里
private static void EagleLanding () {
EagleLandingException("There is a problem with the Eagle!");
System.exit(9999);
}
EagleLandingException(...)
被解释为对名为 EagleLandingException
的方法的调用。但这不是一种方法。它是一个构造函数,它的名称(类名)不在 Java 查找方法名称的 namespace 中。编译错误(实际上)是说“我找不到一个名为 ... 的方法”。
另一个问题是您的EagleLanding
方法没有声明为抛出异常。因此,当您随后尝试在调用者中捕获异常时,编译器会说“这是一个已检查的异常,并且由于未将异常声明为已抛出,因此它不会在此处发生”。因此编译错误。
正确的写法是:
private static void eagleLanding () throws EagleLandingException {
throw new EagleLandingException("There is a problem with the Eagle!");
}
注意事项:
new
导致创建异常对象throw
导致抛出异常。throws
子句声明该方法抛出已检查的异常。throw
之后的语句不可访问。如果你不删除它们,你会得到一个编译错误。 (而且,除了在这样的方法中调用 exit
之外,这是一个坏主意......)在他的回答中,@javaguy 建议不要选中 EagleLandingException
;即声明为 RuntimeException
的子类型。
这样对吗?也许是,也许不是。在您给我们的(高度人为的)背景下,无法确定。
但是,Java 设计人员建议开发人员应遵循一些通用准则:
已检查的异常适用于预期的事件,在这些事件中可能会对异常采取措施。
未经检查的异常用于意外事件(例如错误),其中(通常)程序员可以做的事情不多。
人们用来决定是使用受检异常还是未受检异常的另一个标准是尽量减少样板代码的数量;即 throws
子句。很多人觉得它们很烦人,会竭尽全力避免它们;例如通过将所有自定义异常声明为未检查、将标准异常包装在自定义异常中等等。
我认为最好的路径介于两者之间。仔细考虑将每个异常声明为已检查或未检查的含义,并使用异常层次结构来管理 throws
声明的大小。
关于java - 调试错误 : Exception is never thrown in body of corresponding try statement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40445311/
git count-objects -v 发出的这个警告是什么意思? Google 只给了我 git 本身的来源。 warning: no corresponding .pack: .git/obje
我要MOVE来自 itab1 的字段至 itab2基于它们的字段名称。我试过以下: CLEAR itab2. MOVE-CORRESPONDING itab1 TO itab2. 这是有效的,但只要
我要MOVE来自 itab1 的字段至 itab2基于它们的字段名称。我试过以下: CLEAR itab2. MOVE-CORRESPONDING itab1 TO itab2. 这是有效的,但只要
假设您将一个项目分解为一个 super 项目和一个子模块。它们都有稳定的(主)分支和开发分支。有没有办法设置您的子模块,以便 super 项目从其 dev 分支跟踪子模块的 dev 分支,并从其稳定分
我有一个列表,其中包含两种不同的商品,product-x 和 product-y。 当用户删除 product-x 时,我需要它的等效 product-y 也删除,反之亦然。 我已经尝试了几种不同的方
下面的示例显示了一个 box div 和一个 :after 内容,它应该是一个单独的 block 。 div.box { background-color: #FAA; width:
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 6 年前。 Improve
我使用的是 ABAP 7.4 SP13。 我想在下面的代码中使用“选项 2”,但是如何进行这种深度映射?这里的目标是有一个干净、简单、易于阅读的映射,避免循环。 TYPES: BEGIN OF
我试图充分了解 docker 的功能,以便对我安全地使用它产生合理的信心。对此的一个建议是始终使用 USER Dockerfile 中的语句。在试图理解这一点的影响时,我遇到了一些麻烦。 具体问题:
我在 Flutter Web 应用程序中使用命名路由进行导航。导航到所需的路由时,URL 会更新,但我无法通过 URL 栏直接导航到该路由。每次我尝试在 URL 中添加路径时,它都会将我带到“.../
我重命名了一个文件夹并更新了命名空间声明,但 ReSharper 6 声称命名空间应该反射(reflect)重命名之前的情况。文件位置数据存储在哪里? 最佳答案 检查并确保您的程序集名称与新的命名空间
我正在 Shopify 上创建带有折扣的草稿订单,其返回错误类似于“必须与根据值计算出的值相对应”。 我计算折扣如下: $amount 是订单总金额 (78.99),$rate (30) 是折扣百分比
我正在使用pyspark.ml.classification中的RandomForestClassifier 我在二进制类数据集上运行模型并显示概率。 我的列概率如下: +-----+--------
我正在尝试绘制以不同角度记录的传感器数据。 import pandas as pd import matplotlib.pyplot as plt #create dataframe, each
我的 ASP.NET 页面没有显示控件。它只是显示母版页错误 The page has one or more controls that do not correspond with 代替 关
问题: 我有一大堆日期,我需要按周排列它们。 问题:如何按日期所在的一年中的第几周对日期进行分组? 示例数据集 Date date = new SimpleDateFormat.parse("04/0
我想获取此命令的选项: my_cmd show --value true -D 为此,我需要通过选项两次(由于架构)。在第二遍期间,无法正确检索与其选项对应的参数。在此示例中,在第二次传递期间检索到的
本地代码 native .c #include #include #include jstring Java_com_lab5_oli_myapplication_MainActivity_he
我正在使用 Firebase Python AdminSDK 生成自定义 token ,Javascript 客户端使用该 token 登录 Firebase。当 JS 客户端尝试使用自定义 toke
在上面的 excel 表中,我需要一个公式来提取存在“1”的特定列名,然后在相应的单元格中输入该名称。示例 - 在上面的图像中,对应于 col“Tags”的每一行都包含相应单元格包含“1”的列名。 最
我是一名优秀的程序员,十分优秀!