-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexception_handling.py
57 lines (49 loc) · 1.25 KB
/
exception_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
'''
An exception is an error that happens during execution of a program. When that error occurs,
Python generate an exception that can be handled, which avoids your program to crash.
'''
# If we print the value of x without declaring it then we will get error.
# This error is handled by exception handling
#uncomment the below line to see the error
# print(x)
# try-except
print("The result of try-except")
try:
print(x)
except:
print("Some Error Occured")
print("\nThe result of try-except with specific error")
try:
print (x)
except NameError:
print("Declare the variable first")
# try-except-else
print("\nThe result of try-except-else with error")
try:
print (x)
except:
print("Error Occured")
else:
print("No Error Occured")
print("\nThe result of try-except with no error")
try:
print ("Hello")
except:
print("Error Occured")
else:
print("No Error Occured")
# try-except-finally
print("\nThe result of try-except-finally with error")
try:
print(x)
except:
print("Error Occured")
finally:
print("this is the final statement after error occured")
print("\nThe result of try-except-finally with no error")
try:
print("Hello world")
except:
print("Error Occured")
finally:
print("this is the final statement after no error occured")