-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexceptional_handling.py
67 lines (48 loc) · 1.05 KB
/
exceptional_handling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'''
Exceptions are the errors which being break by the program or user.
We have to handle the exception to excecute our program or software smoothly.
In exception handling we have 3 main blocks.
1. Try block
2. Except block / Catch block
3. Finally block
Keywords to use in Exceptional Handling.
try, except, finally, raise
'''
try:
raise IOError('sadad')
open('f1.txt')
print (5 + str(5))
raise RuntimeError('Sir! Something went wrong.')
except ZeroDivisionError as e:
print (str(e))
except RuntimeError as e:
print (str(e))
except AttributeError as e:
print (str(e))
except TypeError as e:
print (str(e))
except FileNotFoundError as e:
print (str(e))
except Exception as e:
print (str(e))
try:
f = open('file.txt', 'w+')
f.append('asdas')
f.close()
except Exception as e:
print (str(e))
finally:
f.close()
print (f)
'''
Task:
'''
try:
f = open('nmn.txt','w+')
f.append('aaaaaa')
f.close()
except Exception as e:
print (str(e))
finally:
f.close()
print (f)