Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ exception handling in class hierarchies

In a hierarchy of exception classes, What is the correct ordering of catch statements so as to allow exceptions of more than one class from the hierarchy of exception classes to be caught?

Is it most derived to the base or the base to most derived?

like image 459
Zamfir Avatar asked Apr 07 '11 02:04

Zamfir


People also ask

Can you explain the hierarchy of exception handling classes?

The class at the top of the exception class hierarchy is the Throwable class, which is a direct subclass of the Object class. Throwable has two direct subclasses - Exception and Error. The Exception class is used for exception conditions that the application may need to handle.

What is exception exception hierarchy?

Exception HierarchyAll exception and error types are subclasses of class Throwable, which is the base class of the hierarchy. One branch is headed by Exception. This class is used for exceptional conditions that user programs should catch. NullPointerException is an example of such an exception.

Which parent class is root hierarchy in exception handling?

The Throwable class, which is an immediate subclass of Object, is at the root of the exception hierarchy.

What class is at the top of the exception hierarchy give its application?

Thus, Throwable is at the top of the exception class hierarchy. Immediately below Throwable are two subclasses that partition exceptions into two distinct branches. One branch is headed by Exception. This classic used for exceptional conditions that user programs should catch.


1 Answers

Most derived first. Handlers are matched in the order they appear, so you want the most specific ones first.

like image 163
Ernest Friedman-Hill Avatar answered Sep 17 '22 07:09

Ernest Friedman-Hill