博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 捕获多个异常_Python捕获多个异常
阅读量:2532 次
发布时间:2019-05-11

本文共 2501 字,大约阅读时间需要 8 分钟。

python 捕获多个异常

We can use a block to catch exceptions and process them. Sometimes we call a function that may throw multiple types of exceptions depending on the arguments, processing logic etc. In this tutorial, we will learn how to catch multiple exceptions in python.

我们可以使用块来捕获异常并对其进行处理。 有时我们根据参数,处理逻辑等调用一个可能引发多种类型异常的函数。在本教程中,我们将学习如何在python中捕获多种异常。

Python捕获多个异常 (Python Catch Multiple Exceptions)

Let’s say we have a function defined as follows:

假设我们有一个定义如下的函数:

import mathdef square(x):    if int(x) is 0:        raise ValueError('Input argument cannot be zero')    if int(x) < 0:        raise TypeError('Input argument must be positive integer')    return math.pow(int(x), 2)

We can catch both ValueError and TypeError in different except block.

我们可以在不同的区块中捕获ValueErrorTypeError

while True:    try:        y = square(input('Please enter a number\n'))        print(y)    except ValueError as ve:        print(type(ve), '::', ve)    except TypeError as te:        print(type(te), '::', te)

I have put the try-except block in while True loop so that I can run the scenario of catching multiple exceptions.

我将try-except块放入True循环中,以便可以运行捕获多个异常的情况。

Output:

输出:

Please enter a number10100.0Please enter a number-5
:: Input argument must be positive integerPlease enter a number0
:: Input argument cannot be zeroPlease enter a number^DTraceback (most recent call last): File "/Users/pankaj/Documents/github/journaldev/Python-3/basic_examples/python_catch_multiple_exceptions.py", line 15, in y = square(input('Please enter a number\n'))EOFError: EOF when reading a lineProcess finished with exit code 1

在单个except块中捕获多个异常 (Catch Multiple Exceptions in a single except block)

If you notice the except block code, it’s same for both the exception types. If that is the case, we can remove the code redundancy by passing the of exception types in the except block.

如果您注意到例外代码,则这两种异常类型都相同。 如果是这种情况,我们可以通过在except块中传递异常类型的来消除代码冗余。

Here is the rewrite of above code where we are catching multiple exceptions in a single except block.

这是上面代码的重写,其中我们在单个except块中捕获了多个异常。

while True:    try:        y = square(input('Please enter a number\n'))        print(y)    except (ValueError, TypeError) as e:        print(type(e), '::', e)

The output will be exactly the same as earlier. We can use this approach when the code in except block is the same for multiple exceptions being caught.

输出将与之前完全相同。 当except块中的代码对于捕获的多个异常相同时,可以使用这种方法。

. 检出完整的python脚本和更多Python示例。

翻译自:

python 捕获多个异常

转载地址:http://tamzd.baihongyu.com/

你可能感兴趣的文章
1. Hello World - WebService based on Spring
查看>>
IntelliJ IDEA设置统一编码utf-8
查看>>
…… are only available on JDK 1.5 and higher 错误(spring 的jdk版本检测在jdk 8下的修订)...
查看>>
分布式缓存小结
查看>>
uva 437 hdu 1069
查看>>
JAVA大作业汇总2
查看>>
IIR滤波器设计(调用MATLAB IIR函数来实现)(转)
查看>>
分数CSD编码
查看>>
phpstudy配置域名
查看>>
稀疏表示、字典学习和压缩感知(基本概念)
查看>>
新手小白Linux(Centos6.5)部署java web项目(mysql5.7安装及相关操作)
查看>>
ruby实现生产者和消费者
查看>>
node.js 之 http 架设
查看>>
MongoDB 备份与还原
查看>>
Oracle启动与关闭数据库实例
查看>>
Spring day01
查看>>
hihocoder-1740-替换函数
查看>>
Codeforce Round #219 Div2
查看>>
option value的值可以有空格 再试试吧
查看>>
.htaccess to httpd.conf
查看>>