Python:ValueError 和 Exception 之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43605512/
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-19 23:12:57  来源:igfitidea点击:

Python: difference between ValueError and Exception?

pythonexceptionerror-handling

提问by vlad.rad

I am trying to understand what is a difference between raising a ValueError and an Exception. I have tried both in the same code (even in the same branch) and the result was the same - I got an error message.

我试图了解引发 ValueError 和异常之间的区别。我在相同的代码中(甚至在同一个分支中)都尝试过,结果是一样的 - 我收到一条错误消息。

I have made a research on this question on SO, but found no discussion on this. Then I read the documentation of exceptions, and found the following definitionof ValueError:

我已经在 SO 上对这个问题进行了研究,但没有发现对此的讨论。然后我阅读了异常的文档,发现了以下ValueError 的定义

Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as IndexError.

当内置操作或函数接收到类型正确但值不合适的参数时引发,并且这种情况没有用更精确的异常来描述,例如IndexError.

So as I understand, an Exception is a more general term, and ValueError can be applied in some specific cases. But since the results of raising both things are the same, I want to understand, what is the practical meaning of differentiating between a ValueError and an Exception. Python version should be here not relevant. Thank you!

所以据我所知,异常是一个更笼统的术语,ValueError 可以应用于某些特定情况。但是由于两个东西的提高结果是一样的,我想了解区分ValueError和Exception的实际意义是什么。Python 版本应该与此处无关。谢谢!

EDIT: Thanks to your answers I got it, what is the difference between both terms in try-exception construct. But how do they differ in case of just raising them, not excepting?

编辑:感谢您的回答,我明白了,try-exception 构造中的两个术语之间有什么区别。但是,如果只是提高它们,而不是例外,它们有何不同?

raise Exception('blah') 

and

raise ValueError('blah') 

Answering to @PeterWood: in both cases I just got the error message "blah", but in one case it was "Exception: blah", and in the second: "ValueError: blah". And I see in this case no practical difference between them both.

回答@PeterWood:在这两种情况下,我都收到错误消息“等等”,但在一种情况下是“异常:等等”,而在第二种情况下:“ValueError:等等”。在这种情况下,我认为它们之间没有实际区别。

回答by Jean-Fran?ois Fabre

ValueErrorinherits from Exception. You can decide to trap either only ValueError, or Exception, that's what exception inheritance is for.

ValueError继承自Exception. 您可以决定仅捕获ValueErrorException,这就是异常继承的用途。

In this example:

在这个例子中:

try:
    a=12+"xxx"
except Exception:
    # exception is trapped (TypeError)

exception is trapped, all exceptions (except BaseExceptionexceptions) are trapped by the exceptstatement.

异常被捕获,所有异常(BaseException异常除外)都被except语句捕获。

In this other example:

在另一个例子中:

try:
    a=12+"xxx"
except ValueError:
    # not trapped

Here, exception is NOT trapped (TypeErroris not ValueErrorand does not inherit)

在这里,异常没有被捕获(TypeError不是ValueError也不继承)

You generally use specific exceptions to trap only the ones that are likely to occur (best example is IOErrorwhen handling files), and leave the rest untrapped. The danger of catching all exceptions is to get a piece of code that does not crash, but does nothing.

您通常使用特定的异常来只捕获可能发生的异常(最好的例子是IOError处理文件时),而让其余的不被捕获。捕获所有异常的危险在于获得一段不会崩溃但什么都不做的代码。

(editing the answer in response to your edit:) when you raise an exception: you're creating an instance of Exceptionwhich will be filtered out by future except ValueError:statements. the message is different because the representation of the exception (when printed) includes the exception class name.

(编辑答案以响应您的编辑:)当您引发异常时:您正在创建一个实例,Exception该实例将被未来的except ValueError:语句过滤掉。该消息是不同的,因为异常的表示(打印时)包括异常类名称。

回答by Nicolas Flores

You said it, ValueError is a specific Exception. A short example :

你说过,ValueError 是一个特定的异常。一个简短的例子:

try:
    print int("hello world")
except ValueError:
    print "A short description for ValueError"

If you change "hello world" with an int, print int(42), you will not raise the exception.

如果您使用 int 更改“h​​ello world”,则打印 int(42),您将不会引发异常。

You can see doc about exceptions here.

您可以在此处查看有关异常的文档