Thread: error class and exceptions

  1. #1
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916

    error class and exceptions

    I've read the FAQ on try/throw/catch, which is my only knowledge of handling errors like that.

    I'd like to implement an error class in my program, which will basically just recieve the error and output some info about it to a file. (It's just for debugging purposes... fullscreen directx apps are a bit difficult to debug) I was wondering if it's possible to set it up so that I can just throw an exception from any part of the program and it will go to my error class.

    Suggestions?
    Away.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You'll have to use try/catch blocks around anything you expect to throw an exception.

    There is a way to sort-of provide a global handler, but its not a particularly pretty solution. Basically, you define a macro to encapsulate your try/catch blocks, and provide a default catch for it. Something like this (note, you'll also probably want a version that takes a variable argument and assign it the return value if possible):
    Code:
    #include <iostream>
    #include <cstdlib>
    
    // The macro.
    #define HANDLEERR(x)                 \
    try { (x); }                         \
    catch(...) {                         \
    std::cout << "Error." << std::endl; }
    
    // Test program.
    int f()
    {
      std::cout << "In f()..." << std::endl;
      throw -1;
    }
    
    int main()
    {
      HANDLEERR(f());
      std::system("pause");
      return 0;
    }
    Note, this has all the normal side-effects of macros, and can be hard to debug. Anyway it is a solution. My advice is to stick with the manual try/catch blocks wherever you need them.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HttpWebResponse Exceptions
    By Boomba in forum C# Programming
    Replies: 1
    Last Post: 01-03-2008, 12:43 PM
  2. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  3. Exceptions that should terminate. Really.
    By Mario F. in forum C++ Programming
    Replies: 7
    Last Post: 06-26-2007, 10:56 AM
  4. Need advice: catch exceptions or call methods to check bits?
    By registering in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 01:49 PM
  5. Throwing exceptions with constructors
    By nickname_changed in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2003, 09:21 AM