Python:ValueError和Exception之间的区别? [英] Python: difference between ValueError and Exception?

查看:232
本文介绍了Python:ValueError和Exception之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解引发ValueError和Exception之间的区别。我用相同的代码(甚至在同一分支)尝试了两者,结果也一样-我收到一条错误消息。

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.

我对此进行了研究关于SO的问题,但未对此进行讨论。然后,我阅读了异常文档,并发现了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 definition of ValueError:


当内置操作或函数接收到类型正确但值不合适的参数时引发更精确的异常,例如 IndexError

据我所知,例外是更笼统的术语,ValueError可以在某些特定情况下应用。但是,由于提出这两种方法的结果是相同的,所以我想理解,区分ValueError和Exception的实际含义是什么。 Python版本与此处无关。谢谢!

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!

编辑
多亏了您的回答,try-exception中两个术语的区别是什么构造。但是,在只举手而不例外的情况下,它们有何不同?

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?

raise Exception('blah') 

raise ValueError('blah') 

回答@PeterWood:在两种情况下,我都收到错误消息等等,但在一种情况下是 Exception:blah,在第二种情况下是 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.

推荐答案

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

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

在此示例中:

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

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

exception is trapped, all exceptions (except BaseException exceptions) are trapped by the except statement.

在另一个示例中:

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

此处,未捕获异常( TypeError 不是 ValueError 并且不会继承)

Here, exception is NOT trapped (TypeError is not ValueError and does not inherit)

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

You generally use specific exceptions to trap only the ones that are likely to occur (best example is IOError when 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.

(对答案进行编辑以响应您的编辑:)引发异常:您正在创建 Exception 的实例,该实例将由将来的过滤掉,但ValueError:语句除外。该消息有所不同,因为异常的表示形式(在打印时)包括异常类名称。

(editing the answer in response to your edit:) when you raise an exception: you're creating an instance of Exception which 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.

这篇关于Python:ValueError和Exception之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆