template based exception class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nick Keighley

    #1

    template based exception class

    Hi,

    Perhaps I was little ambitious with this template class.

    When I define exception classes for packages I seemed to write code
    similar to this each time:-

    class DecompressionEr ror : public std::runtime_er ror
    {
    public:
    DecompressionEr ror (const std::string& what = "")
    : runtime_error(s td::string("Dec ompression Error ") + what)
    {
    }

    virtual ~DecompressionE rror()
    {
    }
    };


    Any comments on the wisdom of such a class?

    This looked like a good use for templates. Hence:-

    #include <string>
    #include <stdexcept>

    template class<const char* NAMEclass Error: public std::runtime_er ror
    // line 19
    {
    public:

    Error (const std::string& what = "")
    : runtime_error(s td::string(NAME ) + " Error ") + what)
    {

    }



    virtual ~Error()
    {

    }

    };


    int main (void)
    {
    Error ("Pippo") pippo_x;

    try
    {
    throw Error("Pippo");
    }
    catch (Error ("Pippo")& e)
    {
    cerr << e.what();
    }
    }


    This actually crashed the compiler!

    :\bin\error_tem pl.cpp(19) : fatal error C1001: INTERNAL COMPILER ERROR
    (compiler file 'msc1.cpp', line 1188)
    Please choose the Technical Support command on the Visual C++
    Help menu, or open the Technical Support help file for more
    information


    So does my code look ok?


    --
    Nick Keighley

    Programming should never be boring, because anything
    mundane and repetitive should be done by the computer.
    ~Alan Turing

  • Morten V Pedersen

    #2
    Re: template based exception class

    Nick Keighley wrote:
    Hi,
    >
    Perhaps I was little ambitious with this template class.
    >
    When I define exception classes for packages I seemed to write code
    similar to this each time:-
    >
    class DecompressionEr ror : public std::runtime_er ror
    {
    public:
    DecompressionEr ror (const std::string& what = "")
    : runtime_error(s td::string("Dec ompression Error ") + what)
    {
    }
    >
    virtual ~DecompressionE rror()
    {
    }
    };
    >
    >
    Any comments on the wisdom of such a class?
    >
    This looked like a good use for templates. Hence:-
    >
    #include <string>
    #include <stdexcept>
    >
    template class<const char* NAMEclass Error: public std::runtime_er ror
    // line 19
    {
    public:
    >
    Error (const std::string& what = "")
    : runtime_error(s td::string(NAME ) + " Error ") + what)
    {
    >
    }
    >
    >
    >
    virtual ~Error()
    {
    >
    }
    >
    };
    >
    >
    int main (void)
    {
    Error ("Pippo") pippo_x;
    >
    try
    {
    throw Error("Pippo");
    }
    catch (Error ("Pippo")& e)
    {
    cerr << e.what();
    }
    }
    >
    >
    This actually crashed the compiler!
    >
    :\bin\error_tem pl.cpp(19) : fatal error C1001: INTERNAL COMPILER ERROR
    (compiler file 'msc1.cpp', line 1188)
    Please choose the Technical Support command on the Visual C++
    Help menu, or open the Technical Support help file for more
    information
    >
    >
    So does my code look ok?
    >
    >
    --
    Nick Keighley
    >
    Programming should never be boring, because anything
    mundane and repetitive should be done by the computer.
    ~Alan Turing
    >
    Hi Nick,
    I have a couple of quick comments to your code:
    - Check: http://www.comeaucomputing.com/techt...#stringliteral
    - template class<const char* NAME=template<c onst char* NAME>
    - You also need to check you angle brackets vs. parantheses

    // morten

    Comment

    • Stephane Wirtel

      #3
      Re: template based exception class

      And why not:

      #define STRINGIZE(x) XSTRING(x)
      #define XSTRING(x) #x

      #define DECLARE_EXCEPTI ON(name) \
      class name##Exception : public std::runtime_er ror { \
      public: \
      name##Exception ( const std::string & what = "" ) \
      : std::runtime_er ror( std::string(STR INGIZE(name) "Exception" ) + (
      what.empty() ? std::string("") : std::string(" : ")) + what ) { \
      } \
      virtual ~name##Exceptio n() throw() { \
      } \
      }

      DECLARE_EXCEPTI ON(My);
      DECLARE_EXCEPTI ON(Super);

      int main( int argc, char ** argv ) {
      try {
      throw MyException("Th ere is an error");
      }
      catch( const MyException & ex ) {
      std::cerr << ex.what() << std::endl;
      }
      return 0;
      }

      --
      Nick Keighley
      >
      Programming should never be boring, because anything
      mundane and repetitive should be done by the computer.
      ~Alan Turing
      >

      Comment

      • Nick Keighley

        #4
        Re: template based exception class

        Stephane Wirtel wrote:

        you snipped my original question
        And why not:
        >
        #define STRINGIZE(x) XSTRING(x)
        #define XSTRING(x) #x
        >
        #define DECLARE_EXCEPTI ON(name) \
        class name##Exception : public std::runtime_er ror { \
        public: \
        name##Exception ( const std::string & what = "" ) \
        : std::runtime_er ror( std::string(STR INGIZE(name) "Exception" ) + (
        what.empty() ? std::string("") : std::string(" : ")) + what ) { \
        } \
        virtual ~name##Exceptio n() throw() { \
        } \
        }
        >
        DECLARE_EXCEPTI ON(My);
        DECLARE_EXCEPTI ON(Super);
        >
        int main( int argc, char ** argv ) {
        try {
        throw MyException("Th ere is an error");
        }
        catch( const MyException & ex ) {
        std::cerr << ex.what() << std::endl;
        }
        return 0;
        }
        yeah, I know I could have done that. But I thought the idea generally
        was
        to replace function type macros with templates.


        --
        Nick Keighley

        Comment

        • Stephane Wirtel

          #5
          Re: template based exception class

          yeah, I know I could have done that. But I thought the idea generally
          was
          to replace function type macros with templates.
          Why do you want to use the template for this case ?

          The template parameters cannot be 'double' or 'char *'.

          Comment

          • Nick Keighley

            #6
            Re: template based exception class

            Morten V Pedersen wrote:
            Nick Keighley wrote:
            Perhaps I was little ambitious with this template class.

            When I define exception classes for packages I seemed to write code
            similar to this each time:-

            class DecompressionEr ror : public std::runtime_er ror
            {
            public:
            DecompressionEr ror (const std::string& what = "")
            : runtime_error(s td::string("Dec ompression Error ") + what)
            {
            }

            virtual ~DecompressionE rror()
            {
            }
            };


            Any comments on the wisdom of such a class?

            This looked like a good use for templates. Hence:-

            #include <string>
            #include <stdexcept>

            template class<const char* NAMEclass Error: public std::runtime_er ror
            // line 19
            {
            public:

            Error (const std::string& what = "")
            : runtime_error(s td::string(NAME ) + " Error ") + what)
            {

            }

            virtual ~Error()
            {

            }
            };


            int main (void)
            {
            Error ("Pippo") pippo_x;

            try
            {
            throw Error("Pippo");
            }
            catch (Error ("Pippo")& e)
            {
            cerr << e.what();
            }
            }


            This actually crashed the compiler!

            :\bin\error_tem pl.cpp(19) : fatal error C1001: INTERNAL COMPILER ERROR
            (compiler file 'msc1.cpp', line 1188)
            Please choose the Technical Support command on the Visual C++
            Help menu, or open the Technical Support help file for more
            information
            I have a couple of quick comments to your code:
            - Check: http://www.comeaucomputing.com/techt...#stringliteral
            ah! and he became Enlightened

            - template class<const char* NAME=template<c onst char* NAME>
            I've no idea where that extra "class" came from...
            - You also need to check you angle brackets vs. parantheses
            sure do!

            excellent! comp.lang.c++ is worth every penny I pay for it!

            This code:-

            *************** *************** *************** *************** ************
            #include <string>
            #include <stdexcept>
            #include <iostream>

            char x_name[] = "Pippo";


            template <const char* NAMEclass Error: public std::runtime_er ror
            {
            public:

            Error (const std::string& what = "")
            : runtime_error(s td::string(NAME ) + " Error " + what)
            {

            }



            virtual ~Error()
            {

            }

            };


            int main (void)
            {
            Error <x_namepippo_ x;

            try
            {
            throw Error<x_name>(" frodo");
            }
            catch (Error <x_name>& e)
            {
            std::cerr << e.what();
            }

            return 0;
            }

            *************** *************** *************** *************** ************
            produces this output:-
            Pippo Error frodo


            --
            Nick Keighley

            - I wanted to put a check in getGuiExtract() , but it's a void
            method, inherited from the father of the father of the father of
            the
            father of the father of the father of the father of the father...

            So I can't modify its interface.
            [found in code comment]

            Comment

            • Nick Keighley

              #7
              Re: template based exception class

              Stephane Wirtel wrote:

              I tried to pass a string constant as a macro parameter. Stephane
              suggested
              a macro instead
              yeah, I know I could have done that. But I thought the idea generally
              was to replace function type macros with templates.
              >
              Why do you want to use the template for this case ?
              to avoid writing the same code (admittedly not very much) over and over
              again.

              The template parameters cannot be 'double' or 'char *'.
              why would I want it to be double? Why can't it be double. I agree
              the template version looks so awkward I'll probably use the macro


              --
              Nick Keighley

              Comment

              • Stephane Wirtel

                #8
                Re: template based exception class

                I tried to pass a string constant as a macro parameter. Stephane
                suggested
                a macro instead
                There is a trick if you want to use 'char *' in the template parameter,
                you can define a string as like as an 'extern char []'.

                But you can get an error with this example:

                extern const char hello [] = "hello";
                extern const char world [] = "world";

                the variable 'hello' and 'world' are const char [6] ( with '\0' )

                my_template< hello template_hello;
                my_template< world template_world;

                for the compiler is the same type.
                >The template parameters cannot be 'double' or 'char *'.
                >
                why would I want it to be double? Why can't it be double. I agree
                the template version looks so awkward I'll probably use the macro
                double and char * are the only cases where you can't use these types for a
                template parameter.
                If you are interested by the template, you can read "C++ Template : The
                complete guide" of Vandervoorde & Josuttis.

                Stephane

                Comment

                • jimmy

                  #9
                  Re: template based exception class


                  Nick Keighley 写道:
                  Morten V Pedersen wrote:
                  Nick Keighley wrote:
                  >
                  Perhaps I was little ambitious with this template class.
                  >
                  When I define exception classes for packages I seemed to write code
                  similar to this each time:-
                  >
                  class DecompressionEr ror : public std::runtime_er ror
                  {
                  public:
                  DecompressionEr ror (const std::string& what = "")
                  : runtime_error(s td::string("Dec ompression Error ") + what)
                  {
                  }
                  >
                  virtual ~DecompressionE rror()
                  {
                  }
                  };
                  >
                  >
                  Any comments on the wisdom of such a class?
                  >
                  This looked like a good use for templates. Hence:-
                  >
                  #include <string>
                  #include <stdexcept>
                  >
                  template class<const char* NAMEclass Error: public std::runtime_er ror
                  // line 19
                  {
                  public:
                  >
                  Error (const std::string& what = "")
                  : runtime_error(s td::string(NAME ) + " Error ") + what)
                  {
                  >
                  }
                  >
                  virtual ~Error()
                  {
                  >
                  }
                  };
                  >
                  >
                  int main (void)
                  {
                  Error ("Pippo") pippo_x;
                  >
                  try
                  {
                  throw Error("Pippo");
                  }
                  catch (Error ("Pippo")& e)
                  {
                  cerr << e.what();
                  }
                  }
                  >
                  >
                  This actually crashed the compiler!
                  >
                  :\bin\error_tem pl.cpp(19) : fatal error C1001: INTERNAL COMPILER ERROR
                  (compiler file 'msc1.cpp', line 1188)
                  Please choose the Technical Support command on the Visual C++
                  Help menu, or open the Technical Support help file for more
                  information
                  >
                  >
                  I have a couple of quick comments to your code:
                  - Check: http://www.comeaucomputing.com/techt...#stringliteral
                  >
                  ah! and he became Enlightened
                  >
                  >
                  - template class<const char* NAME=template<c onst char* NAME>
                  >
                  I've no idea where that extra "class" came from...
                  >
                  - You also need to check you angle brackets vs. parantheses
                  >
                  sure do!
                  >
                  excellent! comp.lang.c++ is worth every penny I pay for it!
                  >
                  This code:-
                  >
                  *************** *************** *************** *************** ************
                  #include <string>
                  #include <stdexcept>
                  #include <iostream>
                  >
                  char x_name[] = "Pippo";
                  >
                  >
                  template <const char* NAMEclass Error: public std::runtime_er ror
                  {
                  public:
                  >
                  Error (const std::string& what = "")
                  : runtime_error(s td::string(NAME ) + " Error " + what)
                  {
                  >
                  }
                  >
                  >
                  >
                  virtual ~Error()
                  {
                  >
                  }
                  >
                  };
                  >
                  >
                  int main (void)
                  {
                  Error <x_namepippo_ x;
                  >
                  try
                  {
                  throw Error<x_name>(" frodo");
                  }
                  catch (Error <x_name>& e)
                  {
                  std::cerr << e.what();
                  }
                  >
                  return 0;
                  }
                  >
                  *************** *************** *************** *************** ************
                  produces this output:-
                  Pippo Error frodo
                  >
                  >
                  --
                  Nick Keighley
                  >
                  - I wanted to put a check in getGuiExtract() , but it's a void
                  method, inherited from the father of the father of the father of
                  the
                  father of the father of the father of the father of the father...
                  >
                  So I can't modify its interface.
                  [found in code comment]
                  you simply go insane.

                  Comment

                  Working...