Geometric Shape Validation

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.")
            self.length = length
            self.width = width
            print("Rectangle created successfully.")

    rect1 = Rectangle(10, 5)  # Output: "Rectangle created successfully."
    rect2 = Rectangle(-10, 5)  # Output: "Error: Invalid side length."
except Exception as e:


© Let’s Data Science


Unlock AI & Data Science treasures. Log in!