-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path26_Exception_Handling.py
More file actions
93 lines (73 loc) · 1.75 KB
/
26_Exception_Handling.py
File metadata and controls
93 lines (73 loc) · 1.75 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Handling a Simple Exception in Python
n = 10
try:
res = n/0
except ZeroDivisionError:
print("Can't be divided by zero!")
# By using exception as e
n = 12
try:
s = n/0
except ZeroDivisionError as e:
print("msg: ",e)
# Difference Between Exception and Error
# Syntax Error (Error)
# print("Hello world" Missing closing parenthesis
# ZeroDivisionError (Exception)
# n = 10
# res = n / 0
# =====================================================
# Syntax and Usage
# try:
# Code that might raise an exception
# except SomeException:
# Code to handle the exception
# else:
# Code to run if no exception occurs
# finally:
# Code to run regardless of whether an exception occurs
# try, except, else and finally Blocks
try:
n = int(input("Enter a number: "))
res = 100 / n
except ZeroDivisionError as e:
print(e)
except ValueError:
print("Enter a valid number!")
else:
print("Result is: ",res)
finally:
print('Execution completed.')
# Python Catching Exceptions
try:
x = int(input("Enter something: ")) # This will cause ValueError
inv = 1/x
except ValueError:
print("Not valid!")
else:
print("Result: ",inv)
# Catching Multiple Exceptions
a = ["10","Twenty",30]
try:
total = int(a[0]) + int(a[1])
except (ValueError, TypeError) as e:
print("Error: ",e)
except IndexError:
print("Index out of range.")
# Catch-All Handlers and Their Risks
try:
res = "100" / 20
except ArithmeticError as e:
print("Error: ",e)
except:
print("Something went Wrong!")
# Raise an Exception
# raise ExceptionType("Error message")
def set(age):
if age < 0:
raise ValueError("Age Can't be negative.")
print(f"Age set to {age}")
try:
set(-5)
except ValueError as e:
print(e)