Python - 5.Exception Handling
From:http://interactivepython.org/courselib/static/pythonds/Introduction/ExceptionHandling.html
Exception Handling
There are two types of errors that typically occur when writing programs.
- syntax error - simply means that the programmer has made a mistake in the structure of a statement or expression.
For example:
>>> for i in range(10)
SyntaxError: invalid syntax (<pyshell#61>, line 1)
- logic error - , denotes a situation where the program executes but gives the wrong result.
In some cases, logic errors lead to very bad situations such as trying to divide by zero or trying to access an item in a list where the index of the item is outside the bounds of the list. In this case, the logic error leads to a runtime error that causes the program to terminate. These types of runtime errors are typically called exceptions.
>>> anumber = int(input("Please enter an integer "))
Please enter an integer -23
>>> print(math.sqrt(anumber))
Traceback (most recent call last):
File "<pyshell#102>", line 1, in <module>
print(math.sqrt(anumber))
ValueError: math domain error
>>>
- We can handle this exception by calling the print function from within a
try
block.
- We can handle this exception by calling the print function from within a
>>> try:
print(math.sqrt(anumber))
except:
print("Bad Value for square root")
print("Using absolute value instead")
print(math.sqrt(abs(anumber))) Bad Value for square root
Using absolute value instead
4.79583152331
>>>
- It is also possible for a programmer to cause a runtime exception by using the
raise
statement. For example, instead of calling the square root function with a negative number, we could have checked the value first and then raised our own exception. The code fragment below shows the result of creating a newRuntimeError
exception. Note that the program would still terminate but now the exception that caused the termination is something explicitly created by the programmer.
- It is also possible for a programmer to cause a runtime exception by using the
>>> if anumber < 0:
... raise RuntimeError("You can't use a negative number")
... else:
... print(math.sqrt(anumber))
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
RuntimeError: You can't use a negative number
>>> There are many kinds of exceptions that can be raised in addition to theRuntimeError
shown above. See the Python reference manual for a list of all the available exception types and for how to create your own.
Python - 5.Exception Handling的更多相关文章
- 异常处理与MiniDump详解(3) SEH(Structured Exception Handling)
write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie 讨论新闻组及文件 一. 综述 SEH--Structured Exception Handlin ...
- Exception handling 异常处理的本质
异常处理的本质:状态回滚或者状态维护. https://en.wikipedia.org/wiki/Exception_handling In general, an exception breaks ...
- Exception Handling Considered Harmful
异常处理被认为存在缺陷 Do, or do not. There is no try. - Yoda, The Empire Strikes Back (George Lucas) by Jason ...
- Exception Handling引入MVP
异常处理(Exception Handling)是所有系统的最基本的基础操作之一,其它的比如日志(Logging).审核(Auditing).缓存(Caching).事务处理(Transaction) ...
- Unity、Exception Handling引入MVP
什么是MVP?在“MVP初探”里就有讲过了,就是一种UI的架构模式. 简单的描述一下Unity和Exception Handling Application Block: Unity是一个轻量级的可扩 ...
- Exception Handling in ASP.NET Web API webapi异常处理
原文:http://www.asp.net/web-api/overview/error-handling/exception-handling This article describes erro ...
- CoreCLR on Mac:体验managed exception handling
C#测试代码: using System; class Program { static void A() { try { Console.WriteLine("Throwing an ex ...
- Exception Handling Statements (C# Reference)
Exception Handling Statements (C# Reference) C# provides built-in support for handling anomalous sit ...
- Exception Handling in ASP.NET Web API
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErr ...
随机推荐
- pyhton读取 excel表格文件
2019的第一天,忘记昨日之事,迎接新的明天. excel表格文件办公中常用,如通过Python操作这些数据需导入并有序读取这些数据 特随笔,供以后查阅 代码如下: import xlrd # fil ...
- MAC终端TAB自动补全忽略大小写
打开终端,输入: nano .inputrc 在里面粘贴上以下语句: set completion-ignore-case onset show-all-if-ambiguous onTAB: men ...
- Python开发【模块】:aiohttp(一)
AIOHTTP 用于asyncio和Python的异步HTTP客户端/服务器 主要特点: 支持客户端和HTTP服务器. 支持服务器WebSockets和 客户端WebSockets开箱即用,没有回调地 ...
- linux清空历史记录
前几天弄了个Linux服务器做网站,发现在不同终端登陆时,上下键都会出现一堆无用的命令,搜了一下解决方法,做个笔记. 在Linux中,每个用户目录下都有一个.bash_history文件用于保存历史命 ...
- Docker镜像推送(push)到Docker Hub
镜像构建成功后,只要有docker环境就可以使用,但必须将镜像推送到Docker Hub上去.我们之前创建的镜像不符合Docker Hub的tag要求,因为 在Docker Hub注册的用户名是boo ...
- 前端 CSS 三种引入方式
CSS三种引入方式 行内样式 内接样式 外部样式 链接式 导入式 行内样式 就是在标签加上style属性设置样式 <!DOCTYPE html> <html lang="e ...
- 一个canonical标签解决site不在首页的问题
前段时间因为竞价的同事为了方便查看转化路径,在每个关键词都设置了不同的url,具体是直接在标准网址后面加#和参数,例如www.abc.com/#defgh,www.abc.com/#ijklmn,因为 ...
- 时间序列分析工具箱——sweep
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/kMD8d5R/article/details/81977856 作者:徐瑞龙.量化分析师,R语言中文 ...
- JavaScript 全栈工程师培训教程 - 阮一峰
http://www.ruanyifeng.com/blog/2016/11/javascript.html https://github.com/ruanyf/jstraining 我现在的技术方向 ...
- sap 查看自己代码的结构
1:进入系统X3C:然后输入T-CODE bsp_wd_cmpwb