Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. A question for C++ gurus about exception handling

A question for C++ gurus about exception handling

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++career
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Daniel Strigl
    wrote on last edited by
    #1

    Why does the application jumps into the catch (B* ex) and not the catch (D* ex)? The dynamic type of p is D and not B!

    #include "stdafx.h"

    class B
    {};

    class D : public B
    {};

    int _tmain(int argc, _TCHAR* argv[])
    {
    try
    {
    B* p = new D;
    throw p;
    }
    catch (D* ex)
    {
    delete ex;
    printf("catch (D* ex)\n");
    }
    catch (B* ex)
    {
    delete ex;
    printf("catch (B* ex)\n");
    }

    return 0;
    

    }

    Regards, Daniel. -- FIND A JOB YOU LOVE, AND YOU'LL NEVER HAVE TO WORK A DAY OF YOUR LIFE. ;)

    P C 2 Replies Last reply
    0
    • D Daniel Strigl

      Why does the application jumps into the catch (B* ex) and not the catch (D* ex)? The dynamic type of p is D and not B!

      #include "stdafx.h"

      class B
      {};

      class D : public B
      {};

      int _tmain(int argc, _TCHAR* argv[])
      {
      try
      {
      B* p = new D;
      throw p;
      }
      catch (D* ex)
      {
      delete ex;
      printf("catch (D* ex)\n");
      }
      catch (B* ex)
      {
      delete ex;
      printf("catch (B* ex)\n");
      }

      return 0;
      

      }

      Regards, Daniel. -- FIND A JOB YOU LOVE, AND YOU'LL NEVER HAVE TO WORK A DAY OF YOUR LIFE. ;)

      P Offline
      P Offline
      Prakash Nadar
      wrote on last edited by
      #2

      Object instance is of D but object type is B.


      I'll write a suicide note on a hundred dollar bill - Dire Straits

      1 Reply Last reply
      0
      • D Daniel Strigl

        Why does the application jumps into the catch (B* ex) and not the catch (D* ex)? The dynamic type of p is D and not B!

        #include "stdafx.h"

        class B
        {};

        class D : public B
        {};

        int _tmain(int argc, _TCHAR* argv[])
        {
        try
        {
        B* p = new D;
        throw p;
        }
        catch (D* ex)
        {
        delete ex;
        printf("catch (D* ex)\n");
        }
        catch (B* ex)
        {
        delete ex;
        printf("catch (B* ex)\n");
        }

        return 0;
        

        }

        Regards, Daniel. -- FIND A JOB YOU LOVE, AND YOU'LL NEVER HAVE TO WORK A DAY OF YOUR LIFE. ;)

        C Offline
        C Offline
        Curi0us_George
        wrote on last edited by
        #3

        Because p is not a D*. It is a B*, which happens to be pointing to an instance of D. Nonetheless, the type of the pointer has not changed. In other words, it's catching B* because that's what you're throwing. If you want it to catch D*, then you should throw D*. e.g.: throw static_cast(p); It's worth noting that you cannot use dynamic_cast here, because B is not considered a polymorphic type. i.e. It has no virtual functions.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups