Thread: Error handling and file ouput

  1. #1
    Registered User
    Join Date
    Oct 2005

    Error handling and file ouput

    I have a workaround to a problem that really feels like a kludge to me and I'd appreciate any recommendations on how it might be rendered more "conceptually clean".

    I implemented a class exception handler, but it ouputs to two different streams. One goes out to stderr and just tells the user that an exception has occurred, and another goes out to a file handle with a very specific format (actually .dot format for graphviz) so that the user can later turn the error output into a postscript document.

    I thought of creating a separate error file for everytime my program throws an exception, but in the end decided to dump it all into one file. So here's how I implemented it in the header:
    class A {
       static ofstream* os;
       void close();
    Then in the source code:
    static ofstream* A::os = new ofstream(XXX);
       delete os;
    So the program repeatedly instantiates A, and when there's an exception, it just dumps things into "os".

    Then, when main terminates, you have to instantiate a class, call close on it, and exit main. Obviously, this last step isn't necessary since if main terminates, everything's going to be wiped clean, but just to maintain best practices.

    Or maybe you could just
    int main() {
       delete A::os;
    Haven't tried it. But maybe it'll work.

    All of this seems very ugly to me. So any suggestions for improvement are welcome.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    I think you could use the singleton pattern here. Unfortunately I have not grasped just what is the best practice solution for implementing the singleton pattern in C++, so I cannot help you further.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2005
    Thanks for the pointer. Looked up "singleton pattern" and it seems it would be a solution to making my implementation cleaner while making it safer for later users.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM