Implement a class that represents a geometric shape (like a rectangle), and raise custom exceptions for invalid parameters (e.g., negative side lengths, invalid angles).
Example 1:
Input: Shape: "Rectangle", Length: 10, Width: 5 Output: "Rectangle created successfully."
Example 2:
Input: Shape: "Rectangle", Length: -10, Width: 5 Output: "Error: Invalid side length."
Use a class to represent the geometric shape.
class Rectangle:
def __init__(self, length, width):
if length <= 0 or width <= 0:
raise Exception("Error: Invalid side length.")
else:
self.length = length
self.width = width
print("Rectangle created successfully.")
try:
rect1 = Rectangle(10, 5) # Output: "Rectangle created successfully."
rect2 = Rectangle(-10, 5) # Output: "Error: Invalid side length."
except Exception as e:
print(e)
Unlock AI & Data Science treasures. Log in!