Throwing an barring :: Beforehand Barring administration

sportsappsios utilitiesappsios adventuregamesios
 31 December 18:00   


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;
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:
} ///:~


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.



Tags: advance, information, object

 exception, throwing, throw, function, object, return, const, returns, myerror, handling, information, context, char*, , exception handling, exception handling throwing, advance exception handling, exception advance exception,

Share Throwing an barring :: Beforehand Barring administration:
Digg it!   Google Bookmarks   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Article In : Computers & Technology  -  Advance C Plus Plus Topics