Catch and Throw: Not Baseball, but C++ functions

Started by
10 comments, last by Zipster 24 years ago
Ack! Can''t believe I forgot the try {} blocks! You have to put the try blocks around the code you want to test, then put catch() {} after it somewhere. try and catch always go together.

Zipster: Well, you can enclose your current code in main() in a try {} block, and put a catch() statement after the try {} block. Then, from that catch() statement, just call your function. throw() whatever you need that function to use when an error occurs, and the program will clean itself up all the way to main() and let your function handle it. You may want to release DX and then put up a dialog box with the data from the exception that you threw. Basically, whatever you want!

Here''s a little snippet, in case I explained it oddly:


// Whenever you want to throw an exception,
// just fill in a MYEXCEPTIONINFO struct and
// throw it.
struct MYEXCEPTIONINFO {
const char* pSourceFileName;
const char* pSourceLineNumber;

int myinformation; // define anything you want
}

void main()
{
try {
GameInit(); // contains initialization stuff
GameRun(); // contains message loop
GameExit(); // contains uninitialization stuff
}

catch( struct MYEXCEPTIONINFO ) {
// do whatever -- call your function if you like
}
}




- null_pointer
Sabre Multimedia
Advertisement
quote:Original post by Zipster

Thx guys, but i still have one last question: Can''t i just make a function called maybe ErrocFunc, and whenever an error occurs or the code fails, it runs this functions with a few parameters (for determining the error), and that function takes care of cleanup? I mean, instead of using catch and throw? Is that OK, too, or does catch and throw have certain advantages over a personalized function?


Sure, you could just use 1 function if:
(a) you always allocate and deallocate the same stuff no matter how far through the program you are (you won''t )
(b) you always want to either continue or exit immediately upon error (you may wish to do something else before exiting)
(c) you always want to do pretty much the same thing for any error (this ties in with (a).)

Imagine this piece of code:
bool result;result = TrySomething();if (!result)   MainErrorFunc();SomeVar theVariable;result = TrySomethingElse();if (!result)   MainErrorFunc(); 

If the first function fails, your program exits cleanly (assuming everything else outside this example deallocates ok...) But if the second function fails, ''theVariable'' never got properly deallocated, did it? What if the constructor for theVariable opened a log file? You''d want to call the destructor to flush that log file and close it, probably. You couldn''t possibly code all the possible situations into your error function to close down any number of different objects. So you simply ensure that objects destructors clean up after the object (as they should), and the exception handling will call the destructors for you.

This topic is closed to new replies.

Advertisement