17.2.4 Defining a catch
Handler to Process a DivideByZeroException
You saw in Section 7.10 that exceptions are processed by catch
handlers. At least one catch
handler (lines 34–37) must immediately follow each try
block. An exception parameter should always be declared as a reference to the type of exception the catch
handler can process (DivideByZeroException
in this case)—this prevents copying the exception object when it’s caught and allows a catch handler to properly catch derived-class exceptions as well.
When an exception occurs in a try
block, the catch
handler that executes is the first one whose type matches the type of the exception that occurred (i.e., the type in the catch
block matches the thrown exception type exactly or is a ...
Get C++ How to Program, 10/e 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.