| Start |
|---|
| Catching exceptions A Multiple Exception classes which are derived from a common base class can be caught with a single Because the Another possibility is the catch-all handler, which will catch any thrown object: |
| Rethrow (propagate) exception Sometimes you want to do something with the exception you catch (like write to log or print a warning) and let it bubble up to the upper scope to be handled. To do so, you can rethrow any exception you catch: Using To rethrow a managed |
| Best practice: throw by value, catch by const reference In general, it is considered good practice to throw by value (rather than by pointer), but catch by (const) reference. One reason why catching by reference is a good practice is that it eliminates the need to reconstruct the object when being passed to the catch block (or when propagating through to other catch blocks). Catching by reference also allows the exceptions to be handled polymorphically and avoids object slicing. However, if you are rethrowing an exception (like If you are sure that you are not going to do anything to change the exception (like add information or modify the message), catching by const reference allows the compiler to make optimizations and can improve performance. But this can still cause object splicing (as seen in the example above). Warning: Beware of throwing unintended exceptions in |
| std::uncaught_exceptions C++17 introduces Output: |
| Custom exception You shouldn't throw raw values as exceptions, instead use one of the standard exception classes or make your own. Having your own exception class inherited from An example throw catch: As you are not only just throwing a dumb error message, also some other values representing what the error exactly was, your error handling becomes much more efficient and meaningful. There's an exception class that let's you handle error messages nicely : You can inherit from this class too: Note that I haven't overridden the |
Function Try Block for regular function Which is equivalent to Note that for constructors and destructors, the behavior is different as the catch block re-throws an exception anyway (the caught one if there is no other throw in the catch block body). The function |
| Nested exception During exception handling there is a common use case when you catch a generic exception from a low-level function (such as a filesystem error or data transfer error) and throw a more specific high-level exception which indicates that some high-level operation could not be performed (such as being unable to publish a photo on Web). This allows exception handling to react to specific problems with high level operations and also allows, having only error an message, the programmer to find a place in the application where an exception occurred. Downside of this solution is that exception callstack is truncated and original exception is lost. This forces developers to manually include text of original exception into a newly created one. Nested exceptions aim to solve the problem by attaching low-level exception, which describes the cause, to a high level exception, which describes what it means in this particular case.
Possible output: If you work only with exceptions inherited from |
| Function Try Blocks In constructor The only way to catch exception in initializer list: |
Function Try Blocks In destructor Note that, although this is possible, one needs to be very careful with throwing from destructor, as if a destructor called during stack unwinding throws an exception, |
Read Article
https://develop.horje.com/learn/1434/reference