Skip to content

Commit 05c86f1

Browse files
committedDec 22, 2024
Something to commit.
1 parent 6f8df1c commit 05c86f1

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
 
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'''
2+
Error Handling Methods.
3+
1. try
4+
2. except
5+
3. else
6+
4. finally
7+
5. raise
8+
'''
9+
10+
### Type of errors
11+
12+
## 1. Syntax Error
13+
# print("Hikaru Nakamura)
14+
15+
## 2. NameError
16+
# print(a)
17+
18+
## 3. TypeError
19+
# print("Hikka" - 3)
20+
21+
## 4. IndentationError
22+
# if 1 < 2:
23+
# print("HHH")
24+
25+
## 5. keyError
26+
# d = {1:"a", 2:"b", 3:"c"}
27+
# print(d[3])
28+
29+
## 6. zeroDivisionError
30+
# print(400/0)
31+
try:
32+
x = 10/0
33+
print(x)
34+
except Exception as e:
35+
print(type(e))
36+
print(e) # e is class 'ZeroDivisionError'
37+
print(type(str(e)))
38+
print(str(e)) # e is string 'division by zero'
39+
40+
41+
# raise example
42+
# Exception inside message will be shown to user.
43+
def test(a):
44+
if a < 0:
45+
raise Exception("Number should be positive.")
46+
else:
47+
a += 10.
48+
print(a)
49+
# call test
50+
test(-5)
51+
# test(-10)

0 commit comments

Comments
 (0)