Archived

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

amish1234

try, catch, throw

Recommended Posts

I was looking at the source for this gamedev article. I see things such as:
try
{
  RootFrame.Load( pxofobj, pd3ddevice, this );
}
catch(...)
{
//this is releasing the objects used to read from the x file.
  SAFERELEASE( pxofapi );
  SAFERELEASE( pxofenum );
  SAFERELEASE( pxofobj );
  throw;
}
 
I tried looking this stuff up in the MSDN library, but they never really say what throw means. Also, what does (...) mean (right after catch). It seems to be some kind of error checking, but why not use if(FAILED(blah blah blah)) like everyone else? Proceeding on a brutal rampage is the obvious choice.

Share this post


Link to post
Share on other sites
try
{
somecode here...
throw(OUT_OF_BOUNDS, message of some sort)
}
catch(OUT_OF_BOUNDS)
{
do this code to try to fix out of bounds errors
}


this is really usefull for building large classes/libraries where the person using the code doesnt have access to write specialize handlers for each error, therefor you can catch the errors (in the user code) and have them deal with it their own way.

this may be partially correct/incorrect.. im trying to remeber what my comp sci classes taught me between my binge drinking and sleep.

Share this post


Link to post
Share on other sites
try:
Code appearing within the statement block following a try is to be "monitored" for exceptions (ie, exceptions generated within that block are to be handled by the following catch block(s), or re-throw to a higher level.

catch:
This defines an exception handling block. The catch keyword takes a single parameter, function-like, which is the type of exception to catch. catch(...) means catch all exception types. It is possible to have multiple catch blocks following a try, like so:

try
{
// some code
}
catch(ExceptionType1 & e)
{
// some code to handle exceptions of type ExceptionType1
}
catch(ExceptionType2 & e)
{
// some code to handle exceptions of type ExceptionType2
}
catch(...)
{
// some code to handle exceptions of all other types
}


throw:
This is used to generate exceptions in client code:

if(some_irreperable_error)
throw( IrreperableException(/*arguments*/) );

Share this post


Link to post
Share on other sites
Just to add a little to Oluseyi''s explanation, if you don''t actually need to do anything with the object that''s thrown, you don''t need the identifier after the type name. Such as:


  
try
{
// something

}
catch(CSomeException)
{
// handle exception where you don''t need the object

}
catch(CAnotherException & e)
{
// display some error message

e.showErrorMessage();
}


/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

Share this post


Link to post
Share on other sites