python handle exception

1. handle exception

import sys

try:
     a=1/1
except Exception, e:
     print "failed", sys.exc_info()[0]
else:
    print "no exception"
finally:
    print "execute final"

2. print exception

try:
    raise Exception("aaa","bbb")
    #a=1/0
except Exception as e :
     print (type(e))
     print(e.args)
     print (e)
时间: 2024-10-11 23:08:17

python handle exception的相关文章

python 中exception,class学习

python 中exception,class 学习 instroduction: Object2 = Object1 ( like java) if Object1 is class object , then copy by reference; if Object1 is basic type, then copy by value 1. exception 主要结构: try: exception ValueError: exception ZeroDivisionError: exce

Learn Python From 'Head First Python' [3] : Exception

1.the using of exception 2.file opration import os >>> try: data = open('sketch.txt') for each in data: try: (role,message) = each.split(':') print(role + ' says: ' + message, end='') except ValueError: pass data.close() except IOError: print('fi

Python python __def__ Exception AttributeError: "'NoneType' object has no attribute

class Person: '''Represents a person.''' population = 0 def __init__(self,name): '''Initializes the person's data.''' self.name = name print '(Initializing %s)' % self.name Person.population +=1 def __del__(self): '''I am dying.''' print '%s says bye

How to handle exception in managed code and unmanaged code

As we known, try...catch mechanism is a quite common feature for the high level languages like java or C#. Although C++ proclaimed  that it supports this mechanism, the memory management limitation of C++ makes this try...catch function is weak. C++

expect shell python   glpi exception.mac

huawei 用expect来获取AC里面WLAN的MAC 地址名单,运行脚本,信息会输出到屏幕1get.switch.wlan.grep.mac.sh 用正则表达式过滤MAC地址,保存纯MAC到文件中2.exception.py 用来把从AC获取的MAC与GLPI的MAC 进行比较,把不在GLPI里面的MAC输出到文件 安装Python-mysql库 [[email protected] 1123diff.from.ac.glpi]# yum install -y MySQL-python [

python 获取exception 名字

[1] 前提:在自己不知道某个具体的异常名字,但是却要捕捉具体的异常. try: """error code""" -- except Exception, exc: print exc.__class__ -- 版权声明:本文为博主原创文章,未经博主允许不得转载.

【跟我一起学Python吧】python with statement 进阶理解

由于之前有一个项目老是要打开文件,然后用pickle.load(file),再处理...最后要关闭文件,所以觉得有点繁琐,代码也不简洁.所以向python with statement寻求解决方法. 在网上看到一篇文章:http://effbot.org/zone/python-with-statement.htm是介绍with 的,参考着例子进行了理解. 如果经常有这么一些代码段的话,可以用一下几种方法改进: 代码段: set thing up try: do something except

python解决接口测试获取手机验证码问题

最近在做接口测试的时候遇到一个问题,就是有个很重要的接口要用到手机短信验证码,而其他接口都依赖于这个验证码,如果没有短信验证码就不能进行下面接口的测试,所以为了定时的验证线上的接口是否正常,而且又不修改代码,所以就想到一下解决方案,如果大家有了更好方案可以一起交流分享 android 代码 Android在收到短信后会发送一个Action为android.provider.Telephony.SMS_RECEIVED的广播,所以我们只需要写个类继承BroadcastReceiver就可以很容易地

python爬虫笔记

1 import urllib2 2 response = urllib2.urlopen("http://www.baidu.com") 3 html = response.read() 4 5 #eg2 6 import urllib2 7 req = urllib2.Request("http://www.baidu.com") 8 response = urllib2.urllib2(req) 9 the_page = response.read() 10