尝试捕获异常处理方案

发布于 2025-02-12 07:27:51 字数 996 浏览 2 评论 0原文

我正在寻找一个多级错误处理方案,我的测试看起来像这样某种程度上看起来像这样:

void tester() {
    String fileName = Path.of(myDir, "file_1.lo").toString();
    assertThrows(
            Error.class,
            () -> TheCompilerClass.parent_method_called_by_testcase(fileName),
            "Expected parse error to be thrown for this test"
    );
    assertTrue(getStdErr().contains("Failed to compile"));
}

这会调用我开发的代码,而在此外,我试图将某些东西发送回来会导致bool:true:true:true。上面的AssertTrue语句。

我的代码结构就是这样:

parent_method_called_by_testcase() {
  try {

  } catch (Exception e) {
    System.err.println("Failed to compile ");
    //how do I return the right thing? 
    //STDERR statements can't be returned back to test case invoking methoid?
  }
}

child_method {

  //this finds that exception has occurred and should pass it up the chain
  //should this have a throw statement? or just a return?
  if (exception) {
    tell the parent so that parent can tell the testcase
  }
}

I am looking to handle a multi-level error handling scenario, where I have a test looks somehwat like this:

void tester() {
    String fileName = Path.of(myDir, "file_1.lo").toString();
    assertThrows(
            Error.class,
            () -> TheCompilerClass.parent_method_called_by_testcase(fileName),
            "Expected parse error to be thrown for this test"
    );
    assertTrue(getStdErr().contains("Failed to compile"));
}

And this invokes the code I have developed where upon exception I am trying to send back something that will result in bool: True at the assertTrue statement above.

My code structure is like this:

parent_method_called_by_testcase() {
  try {

  } catch (Exception e) {
    System.err.println("Failed to compile ");
    //how do I return the right thing? 
    //STDERR statements can't be returned back to test case invoking methoid?
  }
}

child_method {

  //this finds that exception has occurred and should pass it up the chain
  //should this have a throw statement? or just a return?
  if (exception) {
    tell the parent so that parent can tell the testcase
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

第七度阳光i 2025-02-19 07:27:51

可以说,像您提到的那样的日志线并不是单位测试中通常进行的测试的一部分。更常见的是,我们测试最终功能。例如,如果儿童方法遇到了我们提出异常的情况,那么父方法会抓住这一点吗?换句话说,父方法也会引发异常吗?这是一个断言可能在单位测试中写入的示例。

在这里,根据您的测试,您的,assertthrows表示您期望在此处抛出异常。因此,在您的catch块中,您将进行一些记录,但如果您希望父母扔,则最终扔e,如果孩子扔了。

parent_method_called_by_testcase() {
  try {
    ...
  } catch (Exception e) {
    System.err.println("Failed to compile ");
    throw e;
  }
}

Arguably, log lines like the one you mention are not part of what is generally tested in unit tests. More commonly, we test end functionality. For example, if the child method encounters a case where we throw an exception, does the parent method catch that? In other words, does the parent method throw an exception as well? This is an example of an assertion that one might write in a unit test.

Here, based on your test, your, assertThrows means that you expect the parent method to throw an exception here. So in your catch block, you would do some logging but ultimately throw e if you want the parent to throw if the child does.

parent_method_called_by_testcase() {
  try {
    ...
  } catch (Exception e) {
    System.err.println("Failed to compile ");
    throw e;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文