Skip to content

Commit 8d81877

Browse files
committed
try: add try_except and try_except_else_finally
1 parent e39bc99 commit 8d81877

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

try/try_except.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/python
2+
# Filename: try_except.py
3+
4+
import sys
5+
6+
try:
7+
s = raw_input('Enter something --> ')
8+
except EOFError:
9+
print '\nWhy did you do an EOF on me?'
10+
sys.exit() # exit the program
11+
except:
12+
print '\nSome error/exception occurred.'
13+
# here, we are not exiting the program
14+
15+
print 'Done'

try/try_except_else_finally.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python
2+
3+
'''
4+
try:
5+
Normal execution block
6+
except A:
7+
Exception A handle
8+
except B:
9+
Exception B handle
10+
except:
11+
Other exception handle
12+
else:
13+
if no exception,get here
14+
finally:
15+
print("finally")
16+
'''
17+
18+
19+
class AError(Exception):
20+
"""AError---exception"""
21+
print('AError')
22+
23+
24+
try:
25+
# raise AError
26+
asdas('123')
27+
except AError:
28+
print("Get AError")
29+
except:
30+
print("exception")
31+
else:
32+
print("else")
33+
finally:
34+
print("finally")
35+
print("hello wolrd")

0 commit comments

Comments
 (0)