Python3 捕捉异常
可以通过try/except语句来实现捕获异常,如下:
bpython version 0.15 on top of Python 3.5.1+ /usr/bin/python3
>>> x=3
>>> y=0
>>> x/y
Traceback (most recent call last):
File "<input>", line 1, in <module>
x/y
ZeroDivisionError: division by zero
捕获异常:
这里只输出提示信息:
#!/usr/bin/env python
# encoding: utf-8
"""
@author: kamil.liu @file: except.py
@time: 16-5-30 上午11:40
"""
x = 3
y = 0
try:
print(x/y)
except ZeroDivisionError:
print('The second number can`t be zero')
结果:
/usr/bin/python3.5 /home/kamil/PycharmProjects/untitled/test1/try/except.py
The second number can`t be zero Process finished with exit code 0
不加参数,捕获异常:
如果捕获到异常,不处理,只传递异常,可以调用不带参数的raise,如下事例:
传递异常:
#!/usr/bin/env python
# encoding: utf-8 class MuffledCalculator:
muffled = False
def calc(self,expr):
try:
return eval(expr)
except ZeroDivisionError:
if self.muffled:
print("Division by zero is illegal")
else:raise
calculator = MuffledCalculator()
print(calculator.calc('9/3'))
#calculator.muffled = True
print(calculator.calc('9/0')) """
/usr/bin/python3.5 /home/kamil/PycharmProjects/untitled/test1/try/except.py
3.0
Traceback (most recent call last):
File "/home/kamil/PycharmProjects/untitled/test1/try/except.py", line 16, in <module>
print(calculator.calc('9/0'))
File "/home/kamil/PycharmProjects/untitled/test1/try/except.py", line 8, in calc
return eval(expr)
File "<string>", line 1, in <module>
ZeroDivisionError: division by zero Process finished with exit code 1
"""
不传递异常
#!/usr/bin/env python
# encoding: utf-8 class MuffledCalculator:
muffled = False
def calc(self,expr):
try:
return eval(expr)
except ZeroDivisionError:
if self.muffled:
print("Division by zero is illegal")
else:raise
calculator = MuffledCalculator()
print(calculator.calc('9/3'))
calculator.muffled = True
print(calculator.calc('9/0')) """
/usr/bin/python3.5 /home/kamil/PycharmProjects/untitled/test1/try/except.py
3.0
Division by zero is illegal
None Process finished with exit code 0
"""
除0行为发生,屏蔽被打开,calc会返回None。
Python3 捕捉异常的更多相关文章
- MVC 应用程序级别捕捉异常
捕捉异常: using System; using System.IO; using System.Net; using System.Net.Http; using System.Net.Http. ...
- .NET [MVC] 利用特性捕捉异常
声明:本代码只适用于.NET MVC. 先创建一个类继承ActionFilterAttribute这个抽象类以及实现IExceptionFilter接口,并实现它的方法OnException. 代码如 ...
- .NET WebAPI 利用特性捕捉异常
声明:本方式及代码只使用与.NET Web API. 先创建类继承ExceptionFilterAttribute类型并复写OnException方法. 代码如下: using System; usi ...
- .NET Core[MVC] 利用特性捕捉异常
声明:本方式适用于MVC.本代码只适用于.NET Core MVC. 先创建一个类继承ExceptionFilterAttribute这个抽象类,并override它的方法OnException. 代 ...
- ABP捕捉异常错误代码
在服务层或者CORE层 随便哪里都可以 创建一个捕捉异常的文件夹 里面写一个LonsidException类 继承后面的接口 然后重写继承的方法 这样在ABP项目运行阶段 无论在哪里出现异 ...
- zzw原创_oracle循环中的异常捕捉_捕捉异常后并继续循环
例子如下 set serveroutput on; declare V_SQL VARCHAR2(255); errorCode number; --异常编码 errorMsg var ...
- Java如何使用finally块来捕捉异常?
在Java编程中,如何使用finally块来捕捉异常? 此示例显示如何使用finally块来通过使用e.getMessage()捕获运行时异常(Illegalargumentexception). p ...
- [TimLinux] Python3.6 异常继承关系
Python3.6 异常继承结构 object └── BaseException ├── Exception │ ├── ArithmeticError │ │ ├── Floating ...
- python3 selenium 超时停止加载,并且捕捉异常, 进行下一步【亲测有效】
from selenium import webdriver import os import re class GetPage: def __init__(self, url_path): self ...
随机推荐
- webpack常用插件
extract-text-wepback-plugin 该插件用于把css代码从页面中抽离出来,以link的形式从外部加载 html-webpack-plugin 可以自动快速地生成html文件
- BZOJ 2005: [Noi2010]能量采集
2005: [Noi2010]能量采集 Time Limit: 10 Sec Memory Limit: 552 MBSubmit: 3312 Solved: 1971[Submit][Statu ...
- spring发布和接收定制的事件(spring事件传播)
spring发布和接收定制的事件(spring事件传播) 2012-12-26 20:05 22111人阅读 评论(2) 收藏 举报 分类: 开源技术(如Struts/spring/Hibernat ...
- 我们来八一八阿里云OS的实质和历史
有个姓许的朋友在微信公众号上这样评论: 但是楼主对yunos的了解程度有多少,建议去了解下再评价别人,免费给你普及下:http://www.ithome.com/html/digi/109484.ht ...
- 理解AngularJS生命周期:利用ng-repeat动态解析自定义directive
ng-repeat是AngularJS中一个非常重要和有意思的directive,常见的用法之一是将某种自定义directive和ng-repeat一起使用,循环地来渲染开发者所需要的组件.比如现在有 ...
- strlen 与 sizeof 的区别
void ngx_time_init(void) { ngx_cached_err_log_time.len = sizeof("1970/09/28 12:00:00") - 1 ...
- Tensorflow学习笔记3:TensorBoard可视化学习
TensorBoard简介 Tensorflow发布包中提供了TensorBoard,用于展示Tensorflow任务在计算过程中的Graph.定量指标图以及附加数据.大致的效果如下所示, Tenso ...
- HTML问题集锦
[1]HTML5怎么设置滚动字幕 <marquee direction=up behavior=scroll loop=3 scrollamount=1 scrolldelay=10 align ...
- BPR: Bayesian Personalized Ranking from Implicit Feedback-CoRR 2012——20160421
1.Information publication:CoRR 2012 2.What 商品推荐中常用的方法矩阵因子分解(MF),协同过滤(KNN)只考虑了用户购买的商品,文章提出利用购买与未购买的偏序 ...
- 网页样式——各种炫酷效果持续更新ing...
1.evanyou效果-彩带的实现,效果如下 注:这个主要用的是Canvas画布实现的,点击背景绘制新的图形,代码如下: /*Html代码:*/ <canvas id=">< ...