Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


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

This topic is 6958 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post

Link to post
Share on other sites
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)
SomeVar theVariable;
result = TrySomethingElse();
if (!result)

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.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!