java异常的catch块中是否会捕获断言错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29671796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:25:12  来源:igfitidea点击:

Will an assertion error be caught by in a catch block for java exception?

javajunittry-catchassert

提问by Galet

Code:-

代码:-

try {
    Assert.assertEquals("1", "2");
} catch (Exception e) {
    System.out.println("I am in error block");
}

If the assert statements fails, I would like to capture the error in the catch block. I am trying with the above code and its not happening.

如果断言语句失败,我想在 catch 块中捕获错误。我正在尝试使用上面的代码,但它没有发生。

Will the assertion error be caught by in a catch block for java exception?

断言错误是否会在 java 异常的 catch 块中被捕获?

采纳答案by J Richard Snape

You have almost answered your own question. Your catch block will not catch the AssertionErrorthat the Assertthrows if it fails, because it is an Error(or, more specifically, it extends java.lang.Error). See the docsfor more information on this. Your catch block only catches Throwableobjects that extend java.lang.Exception

你几乎已经回答了你自己的问题。您catch块不会赶上AssertionError的是,Assert如果失败抛出,因为它是一个Error(或者更具体地说,它扩展java.lang.Error)。 有关这方面的更多信息,请参阅文档。您的 catch 块只捕获Throwable扩展的对象java.lang.Exception

If you really want to catch it - you need to use

如果你真的想抓住它 - 你需要使用

catch (AssertionError e) {
...

However, as othershave mentioned, this is a very unusual way to use assertions - they should usually pass and if they fail it is very unusual for you to want to carry on program execution. That's why the failure throws an Errorrather than an Exception. You can read more about (not) catching Errorin this question.

然而,正如其他人提到的,这是使用断言的一种非常不寻常的方式——它们通常应该通过,如果它们失败,您想要继续执行程序是非常不寻常的。这就是为什么失败会抛出一个Error而不是一个Exception. 您可以Error此问题中阅读有关(不)捕获的更多信息。

Are you sure you don't just want a test - if ( variableName == "1")?

你确定你不只是想要一个测试 - if ( variableName == "1")

NBif you are testing unit-test helper code, like a matcher, it might make sense to catch the AssertionError.

注意,如果您正在测试单元测试帮助程序代码,例如匹配器,则捕获AssertionError.

回答by Aakash

Well, I believe you are using JUnit for writing your tests. In that case, you should not catch your Assert.assertEquals()because they should pass for normal test execution. If it throws any exception, it means that your code is not performing as it should.

好吧,我相信您正在使用 JUnit 来编写您的测试。在这种情况下,你不应该抓住你的,Assert.assertEquals()因为它们应该通过正常的测试执行。如果它抛出任何异常,则意味着您的代码未按预期执行。

回答by SpaceCowboy

If you want to catch the errors in that way you need something like the following:

如果您想以这种方式捕获错误,您需要如下内容:

if (num == 1 || num == 2) {
    throw new Exception();
}

You could create your own exception class and pass in the message you want.

您可以创建自己的异常类并传入您想要的消息。

回答by wilmol

If you want to catch both Exceptionand Errorinstances use:

如果您想同时捕获ExceptionError实例,请使用:

...
catch (Throwable t)
{
...
}

Since both Exceptionand Errorextend Throwable.

由于两者ExceptionError扩展Throwable