Jump to content
  • Advertisement
Sign in to follow this  
fathom88

Catching Exceptions Without Try

This topic is 5410 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

I inherited some code which sometimes throws exceptions. Windows handles it by creating a dialog window. I would like to catch the exception myself and handle it in my own fashion. I realize you can do a try{ ---code--- } catch() However, this method would require a lot of code change. Can I catch the signal/message myself using Visual C++? I know in UNIX you can do signal(SIGFPE, myhandler) but it does not work on Windows.

Share this post


Link to post
Share on other sites
Advertisement
This is a horrible, horrible idea. The phrase:
Quote:
However, this method would require a lot of code change.

should set off alarm bells because you are acting in the name of laziness. Laziness is no excuse for poor engineering. Find out what exceptions are thrown and handle them appropriately. If a real exception did occur, it would get silently eaten by your exception handler.

Share this post


Link to post
Share on other sites
read the articles on gotw.ca to do with exception safety.

If you know what functions can throw, wrap them in your own layer which will try/catch and return an error code instead (as I assume that's what you want to use instead).


//old way
int main() {
SomeLegacyFunctionCanThrow();//can cause a crash by throwing LegacyException
return 0;
}




//new way
//returns error code
int WrapperRoundSomeLegacyFunction() {
int error = 0;
try {
SomeLegacyFunctionCanThrow();
}
catch(LegacyException& e) {
//decide what error code to return
}
return error;
}
int main() {
WrapperRoundSomeLegacyFunction();//can now ignore return value (joke!)
return 0;
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!