≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > Python

python异常处理功能解析

人气:603 时间:2018-09-27

这篇文章主要为大家详细介绍了python异常处理功能解析,具有一定的参考价值,可以用来参考一下。

对python这个高级语言感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

本节主要介绍Python中异常处理的原理和主要的形式。

1、什么是异常

Python中用异常对象来表示异常情况。程序在运行期间遇到错误后会引发异常。如果异常对象并未被处理或捕获,程序就会回溯终止执行。

2、抛出异常

raise语句,raise后面跟上Exception异常类或者Exception的子类,还可以在Exception的括号中加入异常的信息。

>>>raise Exception('message')

注意:Exception类是所有异常类的基类,我们还可以根据该类创建自己定义的异常类,如下:

class SomeCustomException(Exception): pass

3、捕捉异常(try/except语句)

try/except语句用来检测try语句块中的错误,从而让except语句捕获异常信息并处理。

一个try语句块中可以抛出多个异常:


# @param python异常处理详解
# @author 四海网|q1010.com 

try:
     x = input('Enter the first number: ')
     y = input('Enter the second number: ')
     print x/y
 except ZeroDivisionError:
     print "The second number can't be zero!"
 except TypeError:
     print "That wasn't a number, was it?"

# End www_512pic_com

 

一个except语句可以捕获多个异常:


# @param python异常处理详解
# @author 四海网|q1010.com 

try:
     x = input('Enter the first number: ')
     y = input('Enter the second number: ')
     print x/y
 except (ZeroDivisionError, TypeError, NameError):  #注意except语句后面的小括号
     print 'Your numbers were bogus...'

# End www_512pic_com

访问捕捉到的异常对象并将异常信息打印输出:


# @param python异常处理详解
# @author 四海网|q1010.com 

try:
     x = input('Enter the first number: ')
     y = input('Enter the second number: ')
     print x/y
 except (ZeroDivisionError, TypeError), e:
     print e

# End www_512pic_com

捕捉全部异常,防止漏掉无法预测的异常情况:


# @param python异常处理详解
# @author 四海网|q1010.com 

try:
     x = input('Enter the first number: ')
     y = input('Enter the second number: ')
     print x/y
 except :
     print 'Someting wrong happened...'

# End www_512pic_com

 

4、else子句。

除了使用except子句,还可以使用else子句,如果try块中没有引发异常,else子句就会被执行。


# @param python异常处理详解
# @author 四海网|q1010.com 

while 1:
     try:
         x = input('Enter the first number: ')
         y = input('Enter the second number: ')
         value = x/y
         print 'x/y is', value
     except:
         print 'Invalid input. Please try again.'
     else:
         break

# End www_512pic_com

 

上面代码块运行后用户输入的x、y值合法的情况下将执行else子句,从而让程序退出执行。

5、finally子句。

不论try子句中是否发生异常情况,finally子句肯定会被执行,也可以和else子句一起使用。finally子句常用在程序的最后关闭文件或网络套接字。


# @param python异常处理详解
# @author 四海网|q1010.com 

try:
     1/0
 except:
     print 'Unknow variable'
 else:
     print 'That went well'
 finally:
     print 'Cleaning up'

# End www_512pic_com

6、异常和函数

如果异常在函数内引发而不被处理,它就会传递到函数调用的地方,如果一直不被处理,异常会传递到主程序,以堆栈跟踪的形式终止。


# @param python异常处理详解
# @author 四海网|q1010.com 

def faulty():
    raise Exception('Someting is wrong!')
def ignore_exception():
    faulty()
     
def handle_exception():
    try:
        faulty()
    except Exception, e:
        print 'Exception handled!',e
 
handle_exception() 
ignore_exception()

# End www_512pic_com

在上面的代码块中,函数handle_exception()在调用faulty()后,faulty()函数抛出异常并被传递到handle_exception()中,从而被try/except语句处理。而ignare_exception()函数中没有对faulty()做异常处理,从而引发异常的堆栈跟踪。

注意:条件语句if/esle可以实现和异常处理同样的功能,但是条件语句可能在自然性和可读性上差一些。

本文来自:http://www.q1010.com/181/1817-0.html

注:关于python异常处理功能解析的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:异常处理

您可能感兴趣的文章

上一篇:Python 闭包与nonlocal的简单示例
下一篇:Python 文件和流的简单示例
热门文章
  • Python 处理Cookie的菜鸟教程(一)Cookie库
  • python之pandas取dataframe特定行列的简单示例
  • Python解决json.dumps错误::‘utf8’ codec can‘t decode byte
  • Python通过pythony连接Hive执行Hql的脚本
  • Python 三种方法删除列表中重复元素的简单示例
  • python爬虫代码示例
  • Python 中英文标点转换示例
  • Python 不得不知的开源项目解析
  • Python urlencode编码和url拼接实现方法
  • python按中文拆分中英文混合字符串的简单示例
  • 最新文章
    • Python利用numpy三层神经网络的简单示例
    • pygame可视化幸运大转盘的简单示例
    • Python爬虫之爬取二手房信息的简单示例
    • Python之time库的简单示例
    • OpenCV灰度、高斯模糊、边缘检测的简单示例
    • Python安装Bs4及使用的简单示例
    • django自定义manage.py管理命令的简单示例
    • Python之matplotlib 向任意位置添加一个子图(axes)的简单示例
    • Python图像标签标注软件labelme分析的简单示例
    • python调用摄像头并拍照发邮箱的简单示例

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。