C++20 sections · 1024 units
Open in Course

Safe Division - The Idea

Exception benefits

The divide function knows it can't divide by zero, so it throws. The caller knows how to recover (show a message, use a default, retry), so it catches.

Use throw to signal an error, try to mark code that might fail, and catch to handle specific error types.

Multiple catch blocks can handle different exception types. Standard exceptions like runtime_error and invalid_argument live in .

Use e.what() to get the error message.