Throwing an barring :: Beforehand Barring administration
| |
Throwing an exception
If you appointment an aberrant bearings in your codethat is, one in which you dont accept abundant advice in the accepted ambience to adjudge what to doyou can forward advice about the absurdity into a beyond ambience by creating an item that contains that advice and throwing it out of your accepted context. This is alleged throwing an exception. Heres what it looks like:
class MyError {
const char* const data;
public:
MyError(const char* const msg = 0) : data (msg) {}
};
void f() {
// Actuality we "throw" an barring object:
throw MyError("something bad happened");
}
int main() {
// As youll see shortly,
// able-bodied wish a "try block" here:
f();
} ///:~
MyError is an accustomed class, which in this case takes a char* as a architect argument. You can use any blazon if you bandy (including congenital types), but usually youll make appropriate classes for throwing exceptions.
The keyword bandy causes a amount of almost bewitched things to happen. First, it creates a archetype of the item youre throwing and, in effect, allotment it from the action absolute the bandy expression, even admitting that item blazon isnt commonly what the action is advised to return. A axis way to anticipate about barring administration is as an alternating acknowledgment apparatus (although you acquisition you can get into agitation if you yield the affinity too far). You can aswell avenue from accustomed scopes by throwing an exception. In any case, a amount is returned, and the action or ambit exits.
Any affinity to action allotment ends there because area you acknowledgment is some abode absolutely altered from area a accustomed action alarm returns. (You end up in an adapted allotment of the codecalled an barring handlerthat ability be far removed from area the barring was thrown.) In addition, any bounded altar created by the time the barring occurs are destroyed. This automated cleanup of bounded altar is generally alleged assemblage unwinding.
|
exception, throwing, throw, function, object, return, const, returns, myerror, handling, information, context, char*, , exception handling, exception handling throwing, advance exception handling, exception advance exception, |
Also see ...
Catching an exception/H1PAs mentioned earlier, one of the advantages of C++ barring administration is that it allows you to apply on the problem youre infact aggravating to break in one place, and then accord with the errors from that cipher in addition place. /P
Exception matching/H1PWhen an barring is thrown, the exception handling arrangement looks through the abutting handlers in the adjustment they arise in the antecedent code. If it finds a match, the barring is advised handled and no added analytic occurs./PPMatc
Uncaught exceptions/H1PAs 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 e
Cleaning up/H1PPart of the abracadabra of barring administration is that you can pop from accustomed program breeze into the adapted barring handler. Accomplishing so wouldnt be useful, however, if things werent bankrupt up appropriately as the barring was thrown. C++ bar
Standard exceptions/H1PThe set of exceptions acclimated with the Accepted C++ library is aswell accessible for your use. About its easier and faster to alpha with a accepted barring chic than to try to ascertain your own. If the accepted chic doesnt do absolutely what you
Catching an exception/H1PAs mentioned earlier, one of the advantages of C++ barring administration is that it allows you to apply on the problem youre infact aggravating to break in one place, and then accord with the errors from that cipher in addition place. /P
Catching an exception/H1PAs mentioned earlier, one of the advantages of C++ barring administration is that it allows you to apply on the problem youre infact aggravating to break in one place, and then accord with the errors from that cipher in addition place. /P
Catching an exception/H1PAs mentioned earlier, one of the advantages of C++ barring administration is that it allows you to apply on the problem youre infact aggravating to break in one place, and then accord with the errors from that cipher in addition place. /P
Catching an exception/H1PAs mentioned earlier, one of the advantages of C++ barring administration is that it allows you to apply on the problem youre infact aggravating to break in one place, and then accord with the errors from that cipher in addition place. /P
Catching an exception/H1PAs mentioned earlier, one of the advantages of C++ barring administration is that it allows you to apply on the problem youre infact aggravating to break in one place, and then accord with the errors from that cipher in addition place. /P