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 ...
随机推荐
- ASMCMD报错解决:sh: /u01/app/11.2.4/grid/bin/clsecho: No such file or directory
sh: /u01/app/11.2.4/grid/bin/clsecho: No such file or directory 在登录asmcmd时报此错误,尝试解决,刷新oracle_sid也不行 ...
- scala-数组/列表
import scala.collection.mutable.ArrayBuffer var ary=Array(1,2,3) println(ary.mkString) println(ary(1 ...
- linux下nodejs的安装
一.下载 https://nodejs.org/en/download/ 然后,解压 二.配置环境变量 配置环境变量:在/etc/profile文件新增:export NODE_HOME=/usr/l ...
- PHP 快速实现大文件上传
简单的上传代码 最简上传代码 <?php move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES[ ...
- C语言编程中pid, tid以及真实pid的关系(转)
add by zhj: 下面是我对pid,tgid,ppid的个人理解 对于ubuntu14.04操作系统,可以在/usr/src/linux-headers-4.4.0-31/include/lin ...
- min-max容斥笔记及例题
这个东西是一个非常好玩的数学工具. $$max(S)=\sum_{T\subset S}(-1)^{|T|-1}min(T)$$ $$max_k(S)=\sum_{T\subset S}(-1)^{| ...
- 【Python】【面试必看】Python笔试题
前言 现在面试测试岗位,一般会要求熟悉一门语言(python/java),为了考验求职者的基本功,一般会出 2 个笔试题,这些题目一般不难,主要考察基本功.要是给你一台电脑,在编辑器里面边写边调试,没 ...
- pip的问题小结
Q:同时安装py2和py3后,pip2不能用 A:使用:python2 -m pip install xxx 代替 pip2 install xxx 命令 Q:怎么用pip更新第三方包 A:pip2 ...
- vue脚手架搭建流程
搭建vue项目之前你需要安装vue的脚手架和node.js,一起去看看怎么搭建一个vue环境吧.(学编程语言最爱看见的就是用这个先写一个helloworld,只想说我对世界友好可是现实是残酷的.... ...
- mysql 数据操作 目录
mysql 记录的增删改查 mysql 数据操作 单表查询 mysql 数据操作 多表查询