The Exception Mechanism
Now let’s see how you can handle problems by using the exception mechanism. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero. Exceptions provide a way to transfer control from one part of a program to another. Handling an exception has three components:
• Throwing an exception
• Catching an exception with a handler
• Using a try
block
A program throws an exception when a problem shows up. For example, you can modify hmean()
in Listing 15.7 to throw an exception instead of call the abort()
function. A throw statement, in essence, is a jump; that is, it tells a program to jump to statements at another location. The throw
keyword indicates ...
Get C++ Primer Plus now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.