Fires of Heaven Guild Message Board  

Go Back   Fires of Heaven Guild Message Board > General forums > Development
User Name
Password
ForumSpy Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 11-08-2007, 01:35 PM   #1 (permalink)
tikkus
Banned
 
Join Date: Nov 2003
Posts: 1,219
-3 Internets
[C++] Try/Throw/Catch

So in my software design class this semester we just wrote a program (CS 216 Program 3) to access data from files, displaying the results to the user. Really easy program, had no problem with it.

Now, fast forward to our current assignment (CS 216 Program 4) where we have to implement the same program, with the major difference being it has to be in perl. I wrote the perl program with ease, but another portion of the assignment was to add file error checking to our 3rd program using try/throw/catch.

This is where I'm at a loss. My functions to retrieve the file contents are in classes and I'm not sure how to set up to throw the errors to the main program. I'm including my source code so any interested persons can sift through it and hopefully help me out.

Oh, and its due tonight at midnight. I figured that this would be the easy part so I put off doing it as long as I possibly could (figured I'd finish it in an hour or so). Little did I know it was going to be the part that killed me.

Anyway, I'm in desperate need of some guidance and if anyone wants to help a brother out just AIM me (breakmyname).
tikkus is offline   Reply With Quote
Old 11-08-2007, 01:38 PM   #2 (permalink)
tikkus
Banned
 
Join Date: Nov 2003
Posts: 1,219
-3 Internets
Oh. Forgot the code.
Attached Files
File Type: zip main.zip (2.1 KB, 8 views)
tikkus is offline   Reply With Quote
Old 11-08-2007, 01:51 PM   #3 (permalink)
Fog
Registered User
 
Join Date: Feb 2006
Posts: 1,568
+1 Internets
Exceptions propagate up the call stack from where they occurred. So if in main.cpp you call

Code:
try { emp_LIST.populateList(empStream); } catch (SomeException& ex) { // do something }
and in empClass.populateList you throw an exception, the executable will look around and say, "okay, am I in a try block?" When it doesn't see a try block in empClass.populateList, it will go back up to the function that called it (main, in this case) and say "any try blocks here?" upon which it will get caught in your catch block.

It looks like that's what your professor wants; the exceptions being thrown when you find an error in the input data in the empClass and storeClass implementations, and then the exceptions being caught in your main() function and handled with appropriate error output.

EDIT: you got owned brosef -----v

Last edited by Fog : 11-08-2007 at 01:54 PM.
Fog is offline   Reply With Quote
Old 11-08-2007, 01:52 PM   #4 (permalink)
Nehrak
Cranky
 
Join Date: Nov 2005
Location: DigiPen
Posts: 803
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 online now   Reply With Quote
Old 11-08-2007, 02:24 PM   #5 (permalink)
tikkus
Banned
 
Join Date: Nov 2003
Posts: 1,219
-3 Internets
Alright, awesome guys. Thanks a bunch.

For this, I'm throwing a string called errorMsg and just returning 1 to the main program. Now the obstacle is the pattern matching, was super easy in perl but will apparently be a hassle for C++. I suppose thats the point of the assignment, haha.

If I have an input (string) with 2 characters that can have all alphanumeric characters, what would be an intuitive way to handle that?

With my first name/last name throw block my conditions are:

Code:
if (tempEmp.lastName.length() > 30 || tempEmp.lastName < "A" || tempEmp.lastName > "z") { errorMsg = "Last Name in employee data file has incorrect format... Ending Program...\n" throw tempEmp.lastName; }
As you can see, I'm relying on the ascii values of the characters to determine valid input. There has to be a better way of looking at that.

Update: Got the thing working! Just need to refine my pattern matching and I'm home free. Thanks so much guys. FoH coders for the win.

Last edited by tikkus : 11-08-2007 at 02:33 PM.
tikkus is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
uberguilds network



All times are GMT -7. The time now is 03:04 PM.


Powered by vBulletin® Version 3.6.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0 RC6