04-14: Safe Math Calculator¶
A command-line calculator built around custom exception classes. Demonstrates try/except/else/finally and the raise statement. Handles division by zero, invalid input, negative square roots, and unsupported operations through a clean exception hierarchy.
Topics covered: custom exception classes · raise · try/except/else/finally · exception inheritance · math.sqrt() · while-loop menu · functions · calculation history
Difficulty: ⭐⭐ Beginner–Intermediate
How to Run¶
Exception Hierarchy¶
Exception
└── CalculatorError
├── DivisionByZeroError
├── InvalidInputError
├── NegativeSquareRootError
└── UnsupportedOperationError
Sample Session¶
Choose: 4
First number : 10
Second number: 0
❌ Math Error: Cannot divide 10 by zero.
Choose: 8
First number : -9
❌ Math Error: Cannot take square root of negative number (-9.0).
Key Concepts Practised¶
| Concept | Where used |
|---|---|
| Custom exception class | DivisionByZeroError(CalculatorError) |
raise ExceptionClass(args) |
Inside divide(), square_root() etc. |
try/except specific type |
except DivisionByZeroError as e: |
else clause |
Runs only when no exception raised |
finally clause |
Always runs — cleanup pattern |