---恢复内容开始---

当发生python不知所措的错误时,python会创建一个异常对象,

如果你编写处理该异常的代码,程序将会继续运行;

如果你未对异常做任何处理,程序将会停止,并显示一个traceback,其中包含异常的报告。

如果让用户看见traceback,这太low了!!


1、处理ZeroDivisionError异常

print(5/0) #0是不能作为除数的
#######################
Traceback (most recent call last):
File "C:/software/PyCharm/alien/images/first.py", line 1, in <module>
print(5/0)
ZeroDivisionError: division by zero

2、使用try-except代码块

当你认为可能要发生错误时,可以编写一个try-except代码块来处理可能引发的异常。

try:
print(5/0)
"""
如果try代码块中的代码运行起来没有问题,程序将直接跳过except代码块
如果代码运行有问题,python将查找except代码块,并运行其中的代码
"""
except ZeroDivisionError:
print("you can't division by zero!!!")

3、else代码块

try-except-else代码块的工作原理大致如下:

python尝试执行try代码块中的代码;只有可能引发异常的代码才会放在try语句中,一些仅在try代码块成功执行时才需要运行的代码,这些代码应放在else代码块中。

print("give me two numbers,and i will divide them!")
print("enter 'q' to quit!")
while True:
first_number = input("first number:")
if first_number == "q":
break
second_number = input("second number:")
if second_number == "q":
break
try:
anwser = int(first_number) / int(second_number)
except ZeroDivisionError:
print("you can't division by zero!!!")
else:
print(anwser)

python基础-----异常问题的更多相关文章

  1. python基础-异常(exception)处理

    python基础-异常(exception)处理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 程序中难免出现错误,而错误分成两种,即语法错误和逻辑错误.语法错误根本过不了pyth ...

  2. python基础--异常,对象和迭代器

    异常处理 面向对象 迭代器和生成器 python异常处理 下面代码触发了一个FileNotFoundError >>> open("notexist.txt") ...

  3. python基础-异常和模块

    异常的定义 #encoding=utf-8 import sys try: 1/0 print "never executed!" except ZeroDivisionError ...

  4. Python基础——异常

    捕捉所有异常 for i in range(10): try: input_number=input('write a number') if input_number=='q': break res ...

  5. 【原】Python基础-异常

    def cacls(x, y): try: return x/y except ZeroDivisionError: print("y can not be zerp") exce ...

  6. Python基础-异常

    异常捕获 常见异常类型 Exception:顶级异常类,大部分异常类都是它的子类.SyntaxError:语法错误TypeError:类型错误ValueError:值错误NameError:找不到名称 ...

  7. Python基础学习笔记(十三)异常

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-exceptions.html Python用异常对象(excep ...

  8. Python基础+模块、异常

    date:2018414+2018415 day1+2 一.python基础 #coding=utf-8 #注释 #算数运算 +(加)  -(减)  *(乘)  /(除)  //(取整)  %(取余) ...

  9. 十九. Python基础(19)--异常

    十九. Python基础(19)--异常 1 ● 捕获异常 if VS异常处理: if是预防异常出现, 异常处理是处理异常出现 异常处理一般格式: try:     <............. ...

随机推荐

  1. noi.openjuge 2.6.90

    http://noi.openjudge.cn/ch0206/90/ 90:滑雪 总时间限制:  1000ms 内存限制:  65536kB 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很 ...

  2. (链表) 83. Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  3. DBMS客户端是否安装:Make sure DBMS client is installed and this required library is available for dynamic loading

    Symptom The full error message is as follows:Error logging in.  Unable to process the database trans ...

  4. python自动化开发-[第七天]-面向对象

    今日概要: 1.继承 2.封装 3.多态与多态性 4.反射 5.绑定方法和非绑定方法 一.新式类和经典类的区别 大前提: 1.只有在python2中才分新式类和经典类,python3中统一都是新式类 ...

  5. CSS样式链接和文字常用属性

    行内: <div style="color:red;"></div> 内嵌<style>div{background-color:red;}&l ...

  6. jsr223 md5

    import java.security.MessageDigest; String content = "xxx"; MessageDigest digest = Message ...

  7. hadoop datanode 启动出错

    FATAL org.apache.hadoop.hdfs.server.datanode.DataNode: Initialization failed for block pool Block po ...

  8. python 存储引擎 mysql(库,表, 行) 单表多表操作 (foreign key) sql_mode pymysql模块讲解

    ##################总结############### mysql 常用数据类型 整型:tinyint  int(42亿条左右)  bigint 小数:float double dec ...

  9. Android中不显示标题

    在网上找的用requestWindowFeature(Window.FEATURE_NO_TITLE)这一句报错. 后来找到另一种方法 1.在res/values/styles.xml中添加如下代码 ...

  10. ES6 In Depth: Arrow functions

    Arrows <script language="javascript"> <!-- document.bgColor = "brown"; ...