-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexception.py
More file actions
146 lines (134 loc) · 5.05 KB
/
Copy pathexception.py
File metadata and controls
146 lines (134 loc) · 5.05 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#error at compile time and is dealt with removing the errors
#Types of error -
#1. Syntax error - compile time error, detected by interpreter
#2. Logical/Semantic error - program runs but gives wrong output
#3. Runtime error - program runs but crashes at some point due to some operation
#Exceptions - unexpected events that occur during program execution that disrupt the normal flow of the program
#A special type of runtime error, which is caught and handled, program can keep running after this.
#exception at runtime and could be predicated and handled with exception handling
#common exceptions - ValueError, TypeError, IndexError, KeyError, FileNotFoundError, ImportError, ZeroDivisionError,
#AttributeError, RuntimeError, RecursionError, NameError, MemoryError, EOFError, KeyboardInterrupt, IndentationError,
#AssertionError
#Raise exceptions with raise keyword
#In python -
#1. try except
#2. try except with else clause, runs if no exception
#3. try except finally, always runs
#4. try except else finally, complete structure
#We can handle different exceptions together or separately with except blocks or use
#global exception class
'''
Programming Errors
├── Compile-Time Errors
│ ├── Syntax Errors
│ ├── Semantic Errors
│ └── Type Errors (in statically typed languages)
│
└── Runtime Errors
├── Logic Errors (wrong output)
├── System Errors (memory, file access)
└── Exceptions (catchable runtime errors)
├── Built-in Exceptions
│ ├── ValueError
│ ├── TypeError
│ ├── IndexError
│ ├── KeyError
│ ├── ZeroDivisionError
│ └── FileNotFoundError
└── Custom Exceptions
'''
'''try:
# code that may raise an exception
x = 10 / 0 # This will raise a ZeroDivisionError
except ZeroDivisionError as e:
print(f"Caught a division by zero error: {e}")
try:
# code that may raise an exception
lst = [1, 2, 3]
print(lst[5]) # This will raise an IndexError
except IndexError as e:
print(f"Caught an index error: {e}")
try:
# code that may raise an exception
num = int("abc") # This will raise a ValueError
except ValueError as e:
print(f"Caught a value error: {e}")
print("Display after the try except")
try:
l = int(input("Enter a number: "))
b = int(input("Enter another number: "))
area = l * b #num
print(f"Area is {area}")
except NameError as e:
print(f"Caught a value error: {e}")
else:
print("No exceptions occurred, area calculated successfully.")
#a try except where we use finally and identify and handle multiple exceptions
try:
num1 = int(input("Enter a number: "))
num2 = int(input("Enter another number: "))
result = num1 / num2
print(f"Result is {result}")
except ZeroDivisionError as e:
print(f"Caught a division by zero error: {e}")
except ValueError as e:
print(f"Caught a value error: {e}")
except Exception as e:
print(f"Caught an unexpected error: {e}")
finally:
print("Execution of the try-except block is complete.")
try:
arr1 = [1, 2, 3]
arr2 = [4, 5, 6]
combined = []
for i in range(len(arr1)):
combined.append(arr1[i] + arr2[i])
print(f"Combined array: {combined}")
except:
print("An error occurred while combining arrays.")
finally:
print("Finished attempting to combine arrays.")
cube=1
try:
num = int(input("Enter a number: "))#here if we put float the int gets the right input of string
#from the prompt
Wrong input VALUE (like "abc" for number) → ValueError
Wrong input TYPE (like passing a list to int()) → TypeError
print(f"{num} ")
except ValueError as e:
print(f"Caught a value error: {e}")
else:
cube = num ** 3
print(f"Cube is {cube}")
finally:
print("Execution of the try-except block is complete.")
#with raise keyword
def check_positive(number):
if number < 0:
raise ValueError("The number must be positive.")
return True
try:
check_positive(-5)
except ValueError as e:
print(f"Caught an error: {e}")'''
#accepts a radius of the circle, calculate are, diameter and circumference of circle, should return multiple values and custom exception handling should be used
#errors to handle value, type error
from legion.gallica import circle_area, diameter, circumference
try:
radius = float(input("Enter the radius of the circle: "))
if not isinstance(radius, (int, float)):
raise TypeError("Radius must be a number.")#The raise statement is used to raise a specific exception manually.
if radius < 0:
raise ValueError("Radius cannot be negative.")
area = circle_area(radius)
dia = diameter(radius)
circ = circumference(radius)
print(f"Area: {area}, Diameter: {dia}, Circumference: {circ}")
except ValueError as e:
print(f"Caught an error: {e}")
except TypeError as e:
print(f"Caught an error: {e}")
else:
print("Circle properties calculated successfully.")
finally:
print("Execution of the circle properties calculation is complete.")