CExeption

This is a discussion on CExeption within the C++ Programming forums, part of the General Programming Boards category; Here's some source code that I've written a while back (it's actually the header source code) How should I start ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    24

    CExeption

    Here's some source code that I've written a while back (it's actually the header source code)

    How should I start to test this header file? (in other words, I need a driver to test teh CExeption class

    Code:
    #ifndef CEXCEPTION_H
    #define CEXCEPTION_H
    
    class cException
    {
       const char* m_ErrorMessage;
       int m_ErrorCode;
    
    public:
       cException(int ErrorCode,const char* ErrorMessage) : 
       m_ErrorCode( ErrorCode ) , 
       m_ErrorMessage( ErrorMessage ) {}
       cException(cException& Ref) :
       m_ErrorCode( Ref.m_ErrorCode ) ,
       m_ErrorMessage( Ref.m_ErrorMessage ) {}
       const char* ErrorMessage() const { return m_ErrorMessage ; }
       int ErrorCode() const { return m_ErrorCode ; }
       ~cException() {}
    };
    #endif //#ifndef CEXCEPTION_H

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Just create a small file, and have a function throw the exception. For example:

    Code:
    #include "cexception.h"
    
    void f ( )
    {
       throw new cException(42, "Oh no, not again.");
    }
    
    int main ( )
    {
       try
       {
          f ( );
       }
       catch ( cException* exception )
       {
          // ... print messages, etc...
          delete exception;
       }
       return 0;
    }
    You just need a function which throws the exception to test it.

    Hope this helps.
    Last edited by Zach L.; 06-18-2003 at 01:43 PM.
    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.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Um, why new? This is not Java:
    Code:
    #include "cexception.h"
    
    void f ( )
    {
       throw cException(42, "Oh no, not again.");
    }
    
    int main ( )
    {
       try
       {
          f ( );
       }
       catch ( cException& exception )
       {
          // ... print messages, etc...
          // now you don't need to delete the exception
       }
       return 0;
    }
    Exception cases are not the time I'd like to think about new and delete.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    One problem of your exception class:
    Code:
    #include "cexception.h"
    
    void f ( )
    {
      char msg[100];
      strcpy(msg, "Oh no, not again.");
       throw cException(42, msg);
    }
    
    int main ( )
    {
       try
       {
          f ( );
       }
       catch ( cException& exception )
       {
          // I need these temp vars
          int i = 0x4F4F4F4F, j = 0x4F4F4F4F, k = 0x4F4F4F4F, l = 0x4F4F4F4F, m = 0x4F4F4F4F;
          cerr << exception.ErrorMessage(); // likely not what you expect
          // might even crash
       }
       return 0;
    }
    The reason is that cException only stores a pointer, which might not point to permanent memory which could get overwritten (in my example I expect it to get overwritten by my temp vars).
    cException should store the data internally, either by allocating in the constructor and deallocating in the destructor or using a std::string.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by CornedBee
    Um, why new? This is not Java:

    Exception cases are not the time I'd like to think about new and delete.
    Didn't know you had to use new with exceptions in Java. Its just a preference to do it that way.
    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.

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    It's far better to catch by reference than heap pointer....apart from not having to delete the pointer in the handler (which Zach's example doesnt do), it's as efficient to catch by reference as it is to catch by pointer.

    Scott Meyers wrote a good section on this in one of his books.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You have to use new for every class in Java, nothing special with exceptions there.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    24

    OK

    Can I use this code to throw an exception using the pre-compiled <exception> header?

    Code:
    #include <exception>
    
    void f ( )
    {
       throw exception(42, "Oh no, not again.");
    }
    
    int main ( )
    {
       try
       {
          f ( );
       }
       catch ( exception& exception )
       {
          // ... print messages, etc...
          // now you don't need to delete the exception
       }
       return 0;
    }

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >>> apart from not having to delete the pointer in the handler (which Zach's example doesnt do)

    Clearly its part of the commented out bit... I'll fix it for clarity. I still say which you choose is a matter of preference.

    >> Can I use this code to throw an exception using the pre-compiled <exception> header?

    The throw/catch syntax is the same. I'm not sure what arguments the constructor of a standard exception takes though (and, I'm not positive, but std::exception may be an ADT).
    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.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    std::exception has two constructors, one taking nothing, the other a const char *.

    But what you can do is derive your own class:
    Code:
    class my_exception : public std::exception
    {
      int errcode;
    public:
      my_exception(int ec, const char *em)
        : std::exception(em), errcode(ec)
      {}
    
      int get_errcode() {
        return errcode;
      }
    };
    I don't know what Zach means by ADT, but if it is, as I guess, Abstract Data Type, I can assure you, exception is not abstract.

    No headers are precompiled (unless you use compiler-specific features), they can only be part of the standard library. I think this is what you mean.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >>> I don't know what Zach means by ADT, but if it is, as I guess, Abstract Data Type...

    Yep, you guessed right.
    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

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