C++20 sections · 1024 units
Open in Course

Safe Division - Algorithm

Implementation approach

Here's your approach for safe division:

1.1. Write a divide function that checks if denominator is zero.

2.2. If zero, throw runtime_error with a descriptive message.

3.3. Otherwise, perform the division and return the result normally.

4.4. In main, wrap the divide call in a try block.

5.5. Add a catch block for runtime_error that prints the error message.

6.6. Add cleanup or continuation code after the try-catch. The program recovers instead of crashing.