Catching exceptions by type

There can be more than one catch block with each try block, which means that you can tailor the exception handling according to the exception type. The types of the parameters in the catch clauses will be tested against the type of the exception in the order that they are declared. The exception will be handled by the first handler that matches the exception type, or is a base class. This highlights the convention to catch the exception object via a reference. If you catch as a base class object a copy will be made, slicing the derived class object. In many cases code, will throw objects of a type derived from the exception class so it means that a catch handler for exception will catch all exceptions.

Since code ...

Get Beginning C++ Programming 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.