-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTry&exception.py
More file actions
48 lines (37 loc) · 1.28 KB
/
Try&exception.py
File metadata and controls
48 lines (37 loc) · 1.28 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
# what is try exception
"""so basically when u make a mistake like syntax error the program usually throws error thats why
we need to use try and exveption methods to avoid errors. the try block holds the code that might have
an eroor and exception basically stands for " a problem has occured" instead throwing errors.
"""
#for example
try:
print(x) # remeber there is no x variable so it should throw an erro
except:
print("An error occured")
"""the try block gets ignored as it will throw an error"""
#there are diffrent types of errors and so are their name
print("Second")
try:
print(y)
except NameError:
print("Its a name error")
except:
print("Some other error")
#in case there was no error the block will execute
try:
print("hello")
except:
print("An error occured")
else:
print("no error occured") # the else block is there for except. if the except condition doesnt prints the else will
"""There is also a finally keyword that can be used to do anything either way if the condition is right or wrong"""
try:
print("hello")
except:
print("An error occured")
finally:
print("The block is finished")
"""There is also a way to raise errors ourselves"""
age = int(input("Enter your age"))
if age < 18:
raise TypeError("You are a minor")