Uncaught exceptions :: Beforehand Barring administration
Uncaught exceptions
As we explained in the alpha of this chapter, barring administration is advised bigger than the acceptable return-an-error-code address because exceptions deceit be ignored. If none of the barring handlers afterward a accurate try block matches an exception, that barring moves to the next-higher context, that is, the action or try block surrounding the try block that did not bolt the exception. (The area of this try block is not consistently accessible at first glance, back its college up in the alarm chain.) This action continues until, at some level, a abettor matches the exception. At that point, the barring is advised caught, and no added analytic occurs.
The terminate( ) function
If no abettor at any akin catches the exception, the appropriate library action terminate( ) (declared in the <exception> header) is automatically called. By default, terminate( ) calls the Accepted C library action abort( ), which abruptly exits the program. On Unix systems, abort( ) aswell causes a amount dump. If abort( ) is called, no calls to accustomed program abortion functions occur, which agency that destructors for all-around and changeless altar do not execute. The terminate( ) action aswell executes if a destructor for a bounded item throws an barring during assemblage unwinding (interrupting the barring that was in progress) or if a all-around or changeless altar architect or destructor throws an exception. In general, do not acquiesce a destructor to bandy an exception.
The set_terminate( ) function
You can install your own terminate( ) action using the accepted set_terminate( ) function, which allotment a arrow to the terminate( ) action you are replacing (which will be the absence library adaptation the first time you alarm it), so you can restore it after if you want. Your custom terminate( ) haveto yield no arguments and accept a abandoned acknowledgment value. In addition, any terminate( ) abettor you install haveto not acknowledgment or bandy an exception, but instead haveto assassinate some array of program-termination logic. If terminate( ) is called, the problem is unrecoverable.
The afterward archetype shows the use of set_terminate( ). Here, the acknowledgment amount is adored and adequate so that the terminate( ) action can be acclimated to advice abstract the area of cipher in which the uncaught barring is occurring:
// Use of set_terminate()
// Aswell shows uncaught exceptions
#include <exception>
#include <iostream>
#include <cstdlib>
using namespace std;
void terminator() {
cout << "I ll be back!" << endl;
exit(0);
}
void (*old_terminate)()
= set_terminate(terminator);
class Blow {
public:
class Bake-apple {};
void f() {
cout << "Botch::f()" << endl;
throw Fruit();
}
~Botch() { bandy c ; }
};
int main() {
try {
Botch b;
b.f();
} catch(...) {
cout << "inside catch(...)" << endl;
}
} ///:~
The analogue of old_terminate looks a bit ambagious at first: it not alone creates a arrow to a function, but it initializes that arrow to the acknowledgment amount of set_terminate( ). Even admitting you ability be accustomed with seeing a semicolon appropriate afterwards a pointer-to-function declaration, actuality its just addition affectionate of capricious and can be initialized if it is defined.
The chic Blow not alone throws an barring central f( ), but aswell in its destructor. As we explained earlier, this bearings causes a alarm to terminate( ), as you can see in main( ). Even admitting the barring abettor says catch(...), which would assume to bolt aggregate and leave no couldcause for terminate( ) to be called, terminate( ) is alleged anyway. In the action of charwoman up the altar on the assemblage to handle one exception, the Blow destructor is called, and that generates a additional exception, banishment a alarm to terminate( ). Thus, a destructor that throws an barring or causes one to be befuddled is usually a assurance of poor architecture or awkward coding.
|
Tags: advance, program, causes, class, block, called, abort, value, library, catch terminate, exception, function, destructor, called, catch, uncaught, return, exceptions, block, throw, throws, handler, botch, pointer, {cout, class, value, causes, handling, library, abort, program, objects, , < <, terminate function, exception the, < endl, exception handling, return value, uncaught exceptions, {cout <, exception handling uncaught, advance exception handling, exceptions advance exception, uncaught exceptions advance, |
Also see ...
PermalinkArticle In : Computers & Technology - Advance C Plus Plus Topics