Skip to content

Commit a9ebafb

Browse files
committed
python的异常处理
1 parent a10ee95 commit a9ebafb

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

python/异常/all_error.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Date : 2015-05-23 10:38:14
4+
# @Author : chenhao (wy.chenhao@qq.com)
5+
# @Link : http://www.xjchenhao.cn
6+
# @Version : $Id$
7+
8+
def convert_to_int(s,base):
9+
try:
10+
return int(s,base)
11+
except Exception, e:
12+
return 'error'
13+
print(convert_to_int('010',1))

python/异常/finally_error.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Date : 2015-05-23 10:40:58
4+
# @Author : chenhao (wy.chenhao@qq.com)
5+
# @Link : http://www.xjchenhao.cn
6+
# @Version : $Id$
7+
8+
import os
9+
def invert(x):
10+
try:
11+
return 1/x
12+
except ZeroDivisionError:
13+
return 'error'
14+
finally:
15+
print('invert(%s) done' % x)
16+
# print(invert(0))

python/异常/variety_error.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Date : 2015-05-23 10:31:02
4+
# @Author : chenhao (wy.chenhao@qq.com)
5+
# @Link : http://www.xjchenhao.cn
6+
# @Version : $Id$
7+
8+
def convert_to_int(s,base):
9+
try:
10+
return int(s,base)
11+
except ValueError:
12+
return 'value error'
13+
except TypeError:
14+
return 'type error'
15+
print(convert_to_int(0,0))
16+
print(convert_to_int('010',1))
17+
print(convert_to_int('010',8))

0 commit comments

Comments
 (0)