python 示例_Python中带有示例的关键字除外
python 示例
Python关键字除外 (Python except keyword)
except is a keyword (case-sensitive) in python, it is used with try... except statement to handle the exception.
除了是python中的一个关键字(区分大小写),它与try ... except语句一起使用来处理异常。
except keyword defines a block which executes if statements are written in try block raise an error.
else关键字定义了一个块,如果在try块中编写了语句,该块将执行,并引发错误。
Note: We can define multiple blocks with except keyword to handle the different types of expectations by mentioning the error/exception names.
注意:我们可以通过提及错误/异常名称,使用except关键字定义多个块以处理不同类型的期望。
Syntax of except keyword
关键字除外的语法
try:
statement(s)-1
except:
statement(s)-2
While executing the statement(s)-1, if there is any exception raises, control jumps to except block and statement(s)-2 executes.
在执行语句-1时 ,如果引发任何异常,则控制跳转到块和语句2 除外 。
Syntax of except keyword with multiple except blocks
具有多个except块的except关键字的语法
try:
statement(s)-1
except Error_Name1:
statement(s)-A
except Error_Name2:
statement(s)-B
except Error_Name3:
statement(s)-C
..
except:
statement(s)-default
While executing the statement(s)-1 if Error_Name1 generates, statements written in Except Error_Name1 (statements(s)-A) executes, and so on... If any error is not mentioned with except block then last except block without any error name executes.
如果Error_Name1生成,则在执行语句-1时,将执行用Error_Name1 以外的语句编写的语句( 语句-A ),依此类推...如果未提及任何错误,则除了block 以外,最后一个exception 都没有错误名称执行。
Example:
例:
Input:
a = 10
b = 0
try:
# no error
result = a%b
print(result)
except:
print("There is an error")
Output:
There is an error
Python的除外关键字示例 (Python examples of except keyword)
Example 1: Find modulus of two number and handle exception, if divisor is 0.
示例1:如果除数为0,则求两个数的模数并处理异常。
# python code to demonstrate example of
# except keyword
# Find modulus of two number and
# handle exception, if divisor is 0
a = 10
b = 3
try:
# no error
result = a%b
print(result)
# assign 0 to b
# an error will occur
b = 0
result = a%b
print(result)
except:
print("There is an error")
Output
输出量
1
There is an error
Example 2: Write an example to handle multiple errors.
示例2:编写示例以处理多个错误。
# python code to demonstrate example of
# except keyword
# Write an example to handle multiple errors
a = 10
b = 3
try:
# del b # uncomment this to test NameError
# no error
result = a%b
print(result)
# assign 0 to b
# an error will occur
b = 0
result = a%b
print(result)
except ZeroDivisionError:
print("Can not divide by 0")
except NameError:
print("A NameError in the code")
except:
print("There is an error")
Output
输出量
1
Can not divide by 0
翻译自: https://www.includehelp.com/python/except-keyword-with-example.aspx
python 示例
python 示例_Python中带有示例的关键字除外相关推荐
- python 示例_Python中带有示例的class关键字
python 示例 Python类关键字 (Python class keyword) class is a keyword (case-sensitive) in python, it is use ...
- c++ cdi+示例_C ++中带有示例的本地类
c++ cdi+示例 C ++中的本地类 (Local Class in C++) In C++, generally a class is declared outside of the main( ...
- np.cosh没有分配_Python中带有示例的math.cosh()方法
np.cosh没有分配 Python math.cosh()方法 (Python math.cosh() method) math.cosh() method is a library method ...
- math.ceil带小数点_Python中带有示例的math.ceil()方法
math.ceil带小数点 Python math.ceil()方法 (Python math.ceil() method) math.ceil() method is a library metho ...
- c语言 函数的参数传递示例_C ++中带有示例的nearint()函数
c语言 函数的参数传递示例 C ++附近的int()函数 (C++ nearbyint() function) nearbyint() function is a library function o ...
- python参数化_Python 中如何实现参数化测试的方法示例
之前,我曾转过一个单元测试框架系列的文章,里面介绍了 unittest.nose/nose2 与 pytest 这三个最受人欢迎的 Python 测试框架. 本文想针对测试中一种很常见的测试场景,即参 ...
- python sep函数_Python中带有print()函数的sep参数
python sep函数 sep parameter stands for separator, it uses with the print() function to specify the se ...
- python用户输入文字_python中用户输入的关键字
广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! python识别用户输入中的某些关键字,然后导出答案. 例如,用户输入 ther ...
- python中定义函数常用关键字_Python 中定义函数的关键字是 _________________ 。_学小易找答案...
[其它]实验4-串和数组-实验任务书.docx [填空题]表达式 'abc' in ['abcdefg'] 的值为______________. [填空题]已知 x = range(1,4) 和 y ...
最新文章
- Android底层网络防火墙,详解Android 利用Iptables实现网络黑白名单(防火墙)
- phpstorm-有关设置
- solid 铝型材框架_solidworks铝型材库
- python画三维图-Python使用matplotlib绘制三维图形示例
- 算法(0)—— 打造一个C开发库
- 代码整洁之道——9、格式化
- 54份运算放大器书籍、文档资料合集(54份 0.46G)
- HTTP、HTTP2.0、SPDY、HTTPS 你应该知道的一些事
- 自然语言处理Java开源包FNLP(FudanNLP)的使用
- 《BBC 跟拍 49 年:穷人与富人的人生七年》
- HC-05 蓝牙 AT指令详解
- 基于springboot 支付宝app端支付,可用于uni-app使用
- webpy+uploadify实现文件异步上传
- (玩转zabbix)硬盘硬件健康状态监控,部件寿命监控
- 定位决定地位,眼界决定境界
- 使用pre-signed URLs通过浏览器上传 无中间件前端直传 minio
- 《智能时代》读书笔记-可以从中学到什么
- 600多个mindmanager模板素材(思维导图模板合集)
- Excel实现双Y轴折线图(四条折线中有两个主轴两个副轴)
- 左中右 三栏式 布局