Catching base class Exception as Reference.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gobi Sakthivel
    New Member
    • Jan 2013
    • 26

    Catching base class Exception as Reference.

    Code:
    class BaseExcep
    {
    protected:
          std::string message;
    
    public:
          BaseExcep(std::string);
          virtual void printException();
    
    };
    
    class DerivedExcep1 : public BaseExcep
    {
    public:
          DerivedExcep1(std::string);
    };
    
    class DerivedExcep2 : public DerivedExcep1
    {
    public:
         DerivedExcep2(std::string);
    };
    
    int main()
    {
    try
    {
      throw DerivedExcep2("Derived2");
    }
    catch(BaseExcep& ex)
    {
    / *
      * Here i need to find which exception type is actually thrown
      * whether it is DerivedExcep1 or DerivedExcep2 or  
      * BaseExcep itself.
      * i cant use dynamic_cast in this, since it is a reference
      * can someone help me out
      */
    }
    Last edited by Gobi Sakthivel; Jul 23 '13, 08:14 AM. Reason: Added proper comments
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    If you need to handle your different exceptions differently then catch them explicitly

    Code:
    int main()
    {
      try
      {
        throw DerivedExcep2("Derived2");
      }
      catch(DerivedExcep1& ex)
      {
        // Handle derived exception 1
      }
      catch(DerivedExcep2& ex)
      {
        // Handle derived exception 2
      }
      catch(BaseExcep& ex)
      {
        // Handle base exception
      }
     
    }

    Comment

    • Gobi Sakthivel
      New Member
      • Jan 2013
      • 26

      #3
      Banfa, Thanks for ur reply, but in my application i have more than 50 Exception classes, and more than 100 files i am using those exceptions, so i cant catch each exception for every case in all files, i can catch the base exception up-to some hierarchy and can do some operation based on the catched exception. For Eg, There are some cases,


      ExceptionHirear chy
      =============== ===

      HeadExcep
      |
      BaseExcep1 BaseExcep2 BaseExcep3 ..........
      | |
      Deri1 Deri2 Deri3 Sub1 Sub2 ............... .......
      Code:
            try
            {  
              try
              {
                try
                {
                    throw DerivedExcep3("aaaaaa");
                }
                catch(DerivedExcep3& ex)
                {
                    ex.setMark();
                    throw;
                }
                 func1();// throws DerivedExcep2
                 func2(); //throws DerivedExcep1
               
              }
              catch(BaseExcep1& ex)
              {
                 BaseExcep3 exc;
                 exc.setCause(ex);
                 throw exc;
              }
              func3();// throws BaseExcep1
              func4();//throws BaseExcep 2
           }
           catch(HeadExcep& ex)
           {
             /*
              * Here i want to know which exception type is actually thrown
              * or else at-least i want to whether the actual Exception 
              * thrown is DerivedExcep4
              * pls let me know your comments on this
              */
           }

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        There is a question here.

        If you have 50 exception types, then you catch each type explicitly as Banfa suggests.

        However, if you have a polymorphic hierarchy then you only need catch a base class reference since the hierarchy interface is in the base class. If you need to know the exact type, then implement a virtual base::what() method. The derived class overrides this method and puts in its own stuff. Like maybe the derived class name.

        BTW: A base class reference is not the same as a derived class reference. You should be catching base class pointers or catching a smart pointer to the base class.

        In any case, nix with the casting. Casting in C++ means you are calling old legacy C code with void* stuff or your C++ design is flawed.

        Have you considered using a Visitor?

        Comment

        • Gobi Sakthivel
          New Member
          • Jan 2013
          • 26

          #5
          @weaknessforcat s, Thanks for ur reply, As u said i have implemented a similar functionality like what(), and it is working fine.:), But u said something about Visitor, what is that, it is a concept in c++?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            A Visitor is a design pattern. A design pattern is a generic solution to problems often encountered in software development.

            Visitor can be used when a class was developed and implemented and later, maybe years later, you find out you need to add a bunch of data members to the original class. Visitor lets you add these members to a new class and then visit the original class o pick up the original data.

            Read this: http://bytes.com/topic/c/insights/67...tterns-visitor

            There are C++ Insights articles for other design patterns.

            Comment

            Working...