Developing Custom Exception Handler :: C++

This is a discussion on Developing Custom Exception Handler :: C++ within the C++ Programming forums, part of the General Programming Boards category; Hi. I would like to create a custom class exception handler. I want to use it for arbitrary exceptions that ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Developing Custom Exception Handler :: C++

    Hi.

    I would like to create a custom class exception handler. I want to use it for arbitrary exceptions that are not part of the exception library.

    I am having a problem with such a class. Here is the code for that exception class.

    -----
    #ifndef CEXCEPTIONHANDLE_H
    #define CEXCEPTIONHANDLE_H

    class ExceptionHandle
    {
    public:
    ExceptionHandle(const char *error) : eMessage(error) {}
    const char *what() const throw()
    {
    return eMessage;
    }

    private:
    const char *eMessage;
    };

    #endif
    -----

    I basic want to *throw* a character string to the exception handle. Afterward, have a catch function to call what().

    Here is an example of a try/catch.

    ----
    try
    {
    function(x);
    }

    catch (ExceptionHandle *e)
    {
    cerr << e->what();
    }
    -----
    function(int X)
    {
    if (X == 0)
    throw ExceptionHandle("Error: X equals zero.");
    }
    -----

    The program I am working on works, but it crashes whenever "throw ExceptionHandle("Error: X equals zero.");" occurs.

    Does anyone know what is wrong with my design and/or implementation?

    Thanks,
    Kuphryn

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    I am pretty sure it's a null reference. You pass a string literal to the exception constructor, but that is local to the line of the constructor call in function, and is no longer on the stack after the constructor finishes.

    Moral of the story, use std::string, and stop worrying about crap like that that probably means nothing to your performance, but a lot in terms of headaches.

    Also, you might want to check out my own implementation for an exception base class for BomberLAN.

    GeneralException.h

    and

    GeneralException.cpp
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. Thanks.

    I believe the problem was due to these pointer related calls:

    -----
    const char *eMessage
    -----

    and/or

    -----
    catch (ExceptionHandle *e)
    -----

    I will do two things.

    1) I will replace "const char *eMessage" with "const string eMessage."
    2) I will replace "catch (ExceptionHandle *e)" with "catch (ExceptionHandle &e)."

    First, is the problem caused by "*e" pointing to something that no longer exists? How can that be if I am throwing an exception each time?

    I can also add "new" to "throw...new."

    -----
    throw new ExceptionHandle("test");
    -----

    Which ones of these changes should *fix* the problem most elegantly?

    Thanks,
    Kuphryn

    P.S. A member at Gamedev mentioned about deriving exception classes from the *exception* library. How important is that?

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Ok, I see the problem.

    You weren't catching the reference, but rather the pointer. You could have thrown a newed exception, but that would make the exception catcher responsible for deletion, and that's not fun.

    Basically what was happening is there was an unhandled exception (trying to catch an ExceptionHandle*, rahter than ExceptionHandle&), and that was crashing your program.

    Deriving your exception class from std::exception has it's own merits... when client code catches exceptions in the form of std::exception, and one of your derived classes is thrown, it will catch your exception. Whether that behavior is wanted or not is up to you, and it's your decision whether or not to derive from std::exception.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Thanks.

    I will catch exceptions via reference from now on. I am used to catching exceptions via value. The exception above is the first time I have tried catching one via pointer.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 07:33 AM
  2. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  3. unexpected exception handler
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-10-2008, 05:49 AM
  4. is such exception handling approach good?
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2007, 08:54 AM
  5. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 02:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21