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.
>>> 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 new RuntimeError exception. Note that the program would still terminate but now the exception that caused the termination is something explicitly created by the programmer.
>>> 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 the RuntimeError 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的更多相关文章

  1. 异常处理与MiniDump详解(3) SEH(Structured Exception Handling)

    write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie 讨论新闻组及文件 一.   综述 SEH--Structured Exception Handlin ...

  2. Exception handling 异常处理的本质

    异常处理的本质:状态回滚或者状态维护. https://en.wikipedia.org/wiki/Exception_handling In general, an exception breaks ...

  3. Exception Handling Considered Harmful

    异常处理被认为存在缺陷 Do, or do not. There is no try. - Yoda, The Empire Strikes Back (George Lucas) by Jason ...

  4. Exception Handling引入MVP

    异常处理(Exception Handling)是所有系统的最基本的基础操作之一,其它的比如日志(Logging).审核(Auditing).缓存(Caching).事务处理(Transaction) ...

  5. Unity、Exception Handling引入MVP

    什么是MVP?在“MVP初探”里就有讲过了,就是一种UI的架构模式. 简单的描述一下Unity和Exception Handling Application Block: Unity是一个轻量级的可扩 ...

  6. Exception Handling in ASP.NET Web API webapi异常处理

    原文:http://www.asp.net/web-api/overview/error-handling/exception-handling This article describes erro ...

  7. CoreCLR on Mac:体验managed exception handling

    C#测试代码: using System; class Program { static void A() { try { Console.WriteLine("Throwing an ex ...

  8. Exception Handling Statements (C# Reference)

    Exception Handling Statements (C# Reference) C# provides built-in support for handling anomalous sit ...

  9. Exception Handling in ASP.NET Web API

    public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErr ...

随机推荐

  1. python,re模块正则

    python没有正则需要导入re模块调用.正则表达式是为了匹配字符串,动态模糊的匹配,只要有返回就匹配到了, 没返回就没匹配到,前面是格式后面是字符串 最常用的匹配语法: re.match()#麦驰, ...

  2. 从网络上筛选"流媒体"的相关文章

    1> 雷神的博客专栏https://blog.csdn.net/leixiaohua1020 [总结]FFMPEG视音频编解码零基础学习方法https://blog.csdn.net/leixi ...

  3. 前端 HTML form表单标签 input标签 type属性 radio 单选框

    <input type="radio"> 单选框 适用于 选择性别按钮网页等 <!DOCTYPE html> <html lang="en& ...

  4. 20170718 关于Mysql 安装于虚拟机Ubuntu中,内网中Windows系统无法访问

    -- 1. 前提Mysql 已经安装在Ubuntu中 -- 2. 防火墙已经关闭 命令确认防护墙状态 -- 3.问题如果Ubuntu是基于Docker容器的环境,是否需要把Docker做端口映射? 解 ...

  5. Java基础知识(JAVA之泛型)

    什么是泛型?为什么要使用泛型? 泛型,即“参数化类型”.一提到参数,最熟悉的就是定义方法时有形参,然后调用此方法时传递实参.那么参数化类型怎么理解呢?顾名思义,就是将类型由原来的具体的类型参数化,类似 ...

  6. MACD:黄白线、红绿柱与0轴关系

    一.MACD的基本问题: (1)参数:统一用12,26,9 (2)MACD   位正.位不正原则 以下四种情况是黄白线.红绿柱与0轴关系的完全分类,行情出不了这四种情况, 即便黄白线在0轴单边反复缠绕 ...

  7. 【剑指offer】字符串的排列

    一.题目: 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 二.思路: ...

  8. vue/cli 3.0 脚手架【进阶】 使用 amfe-flexible 和 postcss-px2rem进行移动端适

    安装vue-cli3   npm install -g @vue/cli 创建项目 vue-cli-test 脚手架-项目-成功-运行项目 基于vue-cli配置移动端自适应 转自:http://hj ...

  9. cmd项目目录结构以及配置文件的升级编写

    一.项目的目录结构: bin:执行文件夹 config:自定义配置文件 lib:公共的模块或者类文件 src:核心业务逻辑代码 二.配置文件的编写 1)config代码如下 from lib.conf ...

  10. [django]主次表如何取出对方数据[主表obj.子表__set()]

    [sql]mysql管理手头手册,多对多sql逻辑 国家--城市例子 class Country(models.Model): name = models.CharField(max_length=3 ...