Topic : Exceptions Handling
Author : Denton Woods
Page : << Previous 2  
Go to page :




class cMain
{
public:
  bool Setup();
  bool Loop();  // The main program loop
  void Close();
};
cMain Main;



In your main() or WinMain() function, you can do something like:

try
{
  Main.Setup();
  Main.Loop();
  Main.Close();
}
catch (Exception &e)
{
  log("Exception thrown:  %s", e.String());  // Just uses any log class you want.

  // Close everything down here and present an error message
}



Your cMain::Loop() function may look something like this:

while (GameActive)
{
  try
  {
    // Perform game loop here
  }
  catch (Exception &e)
  {
    /* Determine if exception thrown was critical, such as a memory error.
    If the exception wasn't critical, just step out of this.  If it was,
    rethrow the exception like either "throw e" or just plain "throw", which
    will rethrow the caught exception. */
  }
}


Conclusion

I've shown you to the best of my abilities how to use C++ exception handling, but only you can decide if you want to use it. The methods presented in this article can help speed development time and make your life a whole lot easier. There are a plenty of topics I failed to cover in this article, and if you want to learn them, I suggest you visit Deep C++.

Page : << Previous 2