Communicable an barring :: Beforehand Barring administration

sportsappsios utilitiesappsios adventuregamesios
 31 December 18:00   


Catching an exception


As 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.


The try block


If youre central a action and you bandy an barring (or a alleged action throws an exception), the action exits in the action of throwing. If you dont wish a bandy to leave a function, you can set up a appropriate block aural the action area you try to break your absolute programming problem (and potentially accomplish exceptions). This block is alleged the try block because you try your assorted action calls there. The try block is an accustomed scope, preceded by the keyword try:


try {
// Cipher that may accomplish exceptions


If you analysis for errors by anxiously analytical the acknowledgment codes from the functions you use, you charge to beleaguer every action alarm with bureaucracy and analysis code, even if you alarm the aforementioned action several times. With barring handling, you put aggregate in a try block and handle exceptions afterwards the try block. Thus, your cipher is a lot easier to address and easier to apprehend because the ambition of the cipher is not abashed with the absurdity checking.


Exception handlers


Of course, the befuddled barring haveto end up some place. This abode is the barring handler, and you charge one barring abettor for every barring blazon you wish to catch. Barring handlers anon chase the try block and are denoted by the keyword catch:


try {
// Cipher that may accomplish exceptions
} catch(type1 id1) {
// Handle exceptions of type1
} catch(type2 id2) {
// Handle exceptions of type2
} catch(type3 id3)
// Etc...
} catch(typeN idN)
// Handle exceptions of typeN
// Accustomed beheading resumes here...


The syntax of a bolt article resembles functions that yield a individual argument. The identifier (id1, id2, and so on) can be acclimated central the handler, just like a action argument, although you can omit the identifier if its not bare in the handler. The barring blazon usually gives you abundant advice to accord with it.


The handlers haveto arise anon afterwards the try block. If an barring is thrown, the exception-handling apparatus goes hunting for the first abettor with an altercation that matches the blazon of the exception. It then enters that bolt clause, and the barring is advised handled. (The seek for handlers stops already the bolt article is found.) Alone the analogous bolt article executes; ascendancy then resumes afterwards the endure abettor associated with that try block.
Notice that, aural the try block, a amount of altered action calls ability accomplish the aforementioned blazon of exception, but you charge alone one handler.
To allegorize using try and catch, the afterward aberration of Nonlocal.cpp replaces the alarm to setjmp( ) with a try block and replaces the alarm to longjmp( ) with a bandy statement.


// Illustrates exceptions
#include <iostream>
using namespace std;
class Bubble {
Rainbow() { cout << "Rainbow()" << endl; }
~Rainbow() { cout << "~Rainbow()" << endl; }
void oz() {
Rainbow rb;
for(int i = 0; i < 3; i++)
cout << "there s no abode like home
throw 47;
int main() {
try {
cout << "tornado, witch, munchkins...
catch (int) {
cout << "Auntie Em! "
<< "I had the strangest dream..."
<< endl;
} ///:~


When the bandy account in oz( ) executes, program ascendancy backtracks until it finds the bolt article that takes an int parameter, at which point beheading resumes with the physique of that bolt clause. The alotof important aberration amid this program and Nonlocal.cpp is that the destructor for the item rb is alleged if the bandy account causes beheading to leave the action oz( ).


There are two basal models in exception-handling theory: abortion and resumption. In abortion (which is what C++ supports), you accept the absurdity is so analytical that theres no way to automatically resume beheading at the point area the barring occurred. In additional words, whoever threw the barring absitively there was no way to deliver the situation, and they dont wish to appear back.


The another error-handling archetypal is alleged resumption, first alien with the PL/I accent in the 1960s . Using resumption semantics agency that the barring abettor is accepted to do something to adjust the situation, and then the faulting cipher is automatically retried, audacious success the additional time. If you wish resumption in C++, you haveto absolutely alteration beheading aback to the cipher area the absurdity occurred, usually by repeating the action alarm that beatific you there in the first place. It is not unusual, therefore, to abode your try block central a while bend that keeps reentering the try block until the aftereffect is satisfactory.


Historically, programmers using operating systems that accurate resumptive barring administration eventually concluded up using termination-like cipher and absence resumption. Although resumption sounds adorable at first, it seems it isnt absolutely so advantageous in practice. One cause may be the ambit that can action amid the barring and its handler; it is one affair to abolish to a abettor thats far away, but to jump to that abettor and then aback afresh may be too conceptually difficult for ample systems on which the barring can be generated from some points.



Tags: advance, argument, block, handle, called, place, catch, statement, resumes, inside, catching

 exception, catch, block, function, handler, handling, place, exceptions, clause, throw, resumption, execution, handle, error, generate, called, rainbow, termination, inside, statement, argument, resumes, catching, handlers, , < <, exception handling, catch clause, handle exceptions, < endl, block and, throw statement, exception handler, cout <, exception handling catching, advance exception handling, exception advance exception,

Share Communicable 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