-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.py
More file actions
76 lines (63 loc) · 1.52 KB
/
errors.py
File metadata and controls
76 lines (63 loc) · 1.52 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
li=[10,20,30,40,50]
x=10
try:
print(x/2)
print(li[4])
print(10/0)
except Exception as e: # exception - run time error
print(e)
except:
print('An error occurred')
print('Hello')
# by using try and exception the whole code will run
# without try and exception the code runs untill the error occurred
# using single exception
x=int(input('please enter a number: '))
try:
print(x)
y = [10, 20, 30]
print(y[0])
print(y[1])
print(y[2])
print(y[3])
except Exception as e:
print(e)
#using multiple exception
print('Good Morning')
x=10
li=[1,2,3,4,5]
try:
print(x/0)
except Exception as e:
print(e)
try:
print(li[5])
except Exception as e:
print(e)
#using multiple exception
try:
x=int(input('please enter a number: '))
print(x)
y = [10, 20, 30]
print(y[0])
print(y[1])
print(y[2])
print(y[3])
except ValueError:
print('enter a valid number')
except NameError:
print('x is invalid')
except IndexError:
print('you are accessing wrong position')
# using multiple exception
x=[10,20,30,40,50]
y=10
try:
print(x[0])
print(y/0)
except IndexError: # named exception, comes b/w try and except
print('Trying to access a non existent index')
except ZeroDivisionError: # named exception, comes b/w try and except
print('Trying to divide a number by 0')
except: # this is default except and generic exception, it should be placed at last
print('An error occurred')