-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtry_except_finally.py
More file actions
62 lines (44 loc) · 1.25 KB
/
try_except_finally.py
File metadata and controls
62 lines (44 loc) · 1.25 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
# The "try" blocks lets you test a block of code for errors
# The "except" bloks lets you handle the error
# The "else" block lets you execute code when there is no error.
# The "finally" block lets you execute code, regardless of the result of the "try" and "except " blocks.
# # Exception Handling
# try:
# print(x)
# except:
# print("An error is occurred")
# # Many Exceptions
# try:
# # x = "HI"
# print(x)
# except NameError:
# print("Variable x is not defined")
# except:
# print("something went wrong")
# # Else
# try:
# x = "HI"
# print(x)
# except NameError:
# print("Variable x is not defined")
# except SyntaxError:
# print("The Invalid syntax in code")
# except:
# print("something went wrong")
# else:
# print("Nothing went wrong")
# # Finally
# try:
# print(x)
# except:
# print("something went worng")
# finally:
# print("The 'try except' is finished")
# # Raise an exception : as a python developer you can choose to throw an exception if a condition occurs.
# # to throw(or raise) an exception, use the "raise" keyword.
# x = -1
# if x < 0:
# raise Exception("sorry, no number below zero")
# x = "Hi"
# if not type(x) is int:
# raise Exception("Only integer are allowed")