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
tryblock.
- 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
raisestatement. 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 newRuntimeErrorexception. 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 theRuntimeErrorshown 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 ...
随机推荐
- JavaScript substr() 字符串截取函数使用详解
substr 定义和用法 substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符. 语法 stringObject.substr(start,length) 如果 length ...
- VS Code 管理 .NET Core解决方案
本练习要使用Visual studio code完成一个包含多个项目的解决方案,包括类库和Web项目.结合Visual Studio Code和.NET Core CLI,创建项目结构如下: pied ...
- android下的样式
android中控件,假如我们把样式都写死在控件的配置文件上的话.一旦改动可谓牵一发而动千军.那么我们能够把样式写在style.xml文件里.然后引用,在API14以上版本号. 该文件位于values ...
- python类型错误:can only concatenate list (not "str") to list
TypeError:can only concatenate list (not "str") to list: 类型错误:只能将list类型和list类型联系起来,而不是str类 ...
- NancyFx-打造小型 WebAPI 與 Microservice 的輕巧利器
https://github.com/NancyFx/Nancy 在做非網站系統整合時,我很愛用一招:寫個 Process 提供 WebAPI 介面給其他系統呼叫,不管你用什麼烏語言鬼平台,怎麼可能找 ...
- [py]py异常应用
异常执行路径 代码参考 try: text = input('请输入 --> ') except EOFError: print('为什么你按下了EOF?') except KeyboardIn ...
- Nginx配置虚拟主机
就是在一台服务器启动多个网站. 如何区分不同的网站: 1.域名不同 2.端口不同 在Nginx的安装目录的conf目录下有个配置文件nginx.conf 1.端口区分: 复制server节点,更改端口 ...
- 【Java】-NO.17.EBook.4.Java.1.014-【疯狂Java讲义第3版 李刚】- Annotation
1.0.0 Summary Tittle:[Java]-NO.17.EBook.4.Java.1.014-[疯狂Java讲义第3版 李刚]- Annotation Style:EBook Serie ...
- 【Python】-NO.98.Note.3.Python -【Python3 解释器、运算符】
1.0.0 Summary Tittle:[Python]-NO.98.Note.3.Python -[Python3 解释器] Style:Python Series:Python Since:20 ...
- jq closet的使用,找到距离最近的一个父元素;