View Single Post
Old 11-08-2007, 01:52 PM   #4 (permalink)
Nehrak
Crankier than the Kong
 
Join Date: Nov 2005
Location: DigiPen
Posts: 870
So here's the basics of it:

In the main program you're going to do the following:

Code:
try { // Do stuff that might cause errors here. } catch(exception e) { // Respond to the exception e that you caught. }
For example, in main.cpp you have emp_LIST.populate_list(). If something goes wrong while reading fin within that function, you'd want to throw an exception. (Say, throw("Read bad data, expected ")). As soon as you throw this, the function immediately terminates and goes back to the calling function (main() in this case). Execution would immediately progress to the catch() block listed above, where you see if the exception thrown is something you can handle. In this case, likely a string, but you can make it more sophisticated as you want (yes, you could conceivably make a class that basically was an error message, then throw an object with all the relevant error data your assignment specs needed; just make sure to catch that particular data type).


So as a basic example (assume all relevant #includes, using directives, etc.):

Code:
int main() { Object o; try { o.DoSomething(); // This may throw an ErrorObject saying what happened. Execution goes right to the catch block and couts the error. o.DoSomethingElse(); // This may throw an ErrorObject too! Also goes directly to the catch block. } catch (ErrorObject &e) { cout << e.error() << endl; } } // The following is in some other file, presumably. void Object::DoSomething() { throw ErrorObject("I suck"); } void Object::DoSomethingElse() { throw ErrorObject("Too much penis."); }
Any questions, take two compiles and call me in about two hours. My CS class is in 10 minutes. ^^
Nehrak is offline   Reply With Quote

 
Uberguilds Network