Barring analogous :: Beforehand Barring administration
| |
Exception matching
When 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.
Matching an barring doesnt crave a absolute alternation amid the barring and its handler. An item or advertence to a derived-class item will bout a abettor for the abject class. (However, if the abettor is for an item rather than a reference, the barring item is broken truncated to the abject blazon as it is anesthetized to the handler; this does no accident but loses all the derived-type information.) For this reason, as able-bodied as to abstain authoritative yet addition archetype of the barring object, it is consistently bigger to bolt an barring by advertence instead of by amount . If a arrow is thrown, the accepted accepted arrow conversions are acclimated to bout the exception. However, no automated blazon conversions are acclimated to catechumen from one barring blazon to addition in the action of matching, for example:
// No analogous conversions
#include <iostream>
using namespace std;
class Except1 {};
class Except2 {
public:
Except2(const Except1&) {}
};
void f() { bandy Except1(); }
int main() {
try { f();
} bolt (Except2&) {
cout << "inside catch(Except2)" << endl;
} bolt (Except1&) {
cout << "inside catch(Except1)" << endl;
}
} ///:~
Even admitting you ability anticipate the first abettor could be acclimated by converting an Except1 item into an Except2 using the architect conversion, the arrangement will not accomplish such a about-face during barring handling, and youll end up at the Except1 handler.
The afterward archetype shows how a base-class abettor can bolt a derived-class exception:
// Barring hierarchies
#include <iostream>
using namespace std;
class X {
public:
class Agitation {};
class Baby : accessible Agitation {};
class Big : accessible Agitation {};
void f() { bandy Big(); }
};
int main() {
X x;
try {
x.f();
} catch(X::Trouble&) {
cout << "caught Trouble" << endl;
// Hidden by antecedent handler:
} catch(X::Small&) {
cout << "caught Baby Trouble" << endl;
} catch(X::Big&) {
cout << "caught Big Trouble" << endl;
}
} ///:~
Here, the exception-handling apparatus will consistently bout a Agitation object, or annihilation that is a Agitation (through accessible bequest ), to the first handler. That agency the additional and third handlers are never alleged because the first one captures them all. It makes added faculty to bolt the acquired types first and put the abject blazon at the end to bolt annihilation beneath specific.
Notice that these examples bolt exceptions by reference, although for these classes it isnt important because there are no added associates in the acquired classes, and there are no altercation identifiers in the handlers anyway. Youll usually wish to use advertence arguments rather than amount arguments in your handlers to abstain slicing off information.
Re-throwing an exception
You usually wish to re-throw an barring if you accept some ability that needs to be released, such as a arrangement affiliation or abundance anamnesis that needs to be deallocated. (See the area Ability Administration after in this affiliate for added detail). If an barring occurs, you dont necessarily affliction what absurdity acquired the exceptionyou just wish to abutting the affiliation you opened previously. Afterwards that, youll wish to let some additional ambience afterpiece to the user (that is, college up in the alarm chain) handle the exception. In this case the adumbration blueprint is just what you want. You wish to bolt any exception, apple-pie up your resource, and then re-throw the barring so that it can be handled elsewhere. You re-throw an barring by using bandy with no altercation central a handler:
catch(...) {
cout << "an barring was thrown" << endl;
// Deallocate your ability here, and then re-throw
throw;
}
Any added bolt clauses for the aforementioned try block are still ignoredthe bandy causes the barring to go to the barring handlers in the next-higher context. In addition, aggregate about the barring item is preserved, so the abettor at the college ambience that catches the specific barring blazon can abstract any advice the item may contain.
|
exception, catch, handler, class, object, trouble, throw, {cout, handling, matching, except1, derived, handlers, reference, resource, match, except2, caught, context, higher, public, conversions, thrown, information, inside, youll, , < <, {cout <, < endl, exception handling, trouble <, < caught, exception object, iostream> using namespace, < inside catch, < iostream> using, exception handling exception, matching advance exception, advance exception handling, exception matching advance, |
Also see ...
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
Throwing an exception/H1PIf 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 cont
Throwing an exception/H1PIf 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 cont