-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path70_Python_Try_Except.py
More file actions
55 lines (48 loc) · 930 Bytes
/
70_Python_Try_Except.py
File metadata and controls
55 lines (48 loc) · 930 Bytes
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
# Python Try Except
print("Python Try Except")
# Exception Handling
print("\nException Handling:")
print("Example:")
try:
print(x)
except:
print("An exception occurred.")
# Many Exceptions
print("\nMany Exceptions:")
print("Example:")
try:
print(x)
# print("x" + 10)
except NameError:
print("x is not defined!")
except:
print("Something else is wrong.")
# Using Else
print("\nUsing Else:")
print("Example:")
try:
print("Hi")
except:
print("Something went wrong!")
else:
print("Nothing went wrong!")
# Finally block
print("\nFinally block:")
try:
print(x)
except:
print("Something went wrong!")
finally:
print("The 'try except' is finished.")
# Raising an exception
print("\nRaising an exception:")
print("Example 1:")
x = -1
if x < 0:
# pass
raise Exception("Oops, number is below 0.")
print("Example 2:")
num = "hi"
if not type(num) is int:
pass
# raise TypeError("Only integers allowed!")