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. Other Discussions
  3. Clever Code
  4. C# rots your C++ brain [modified]

C# rots your C++ brain [modified]

Scheduled Pinned Locked Moved Clever Code
questioncsharpc++testingbeta-testing
7 Posts 6 Posters 22 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.
  • J Offline
    J Offline
    Jorgen Sigvardsson
    wrote on last edited by
    #1

    class MyException : public std::exception {
    public:
    const char* what() const { // overrides std::exception::what()
    return "my message";
    }
    };

    ...

    try {
    try {
    throw MyException();
    } catch(std::exception& ex) {
    throw ex;
    }
    } catch(std::exception& ex) {
    std::cout << ex.what() << std::endl;
    }

    What is the printout? I found it by accident, as I was testing how some code I just wrote, works in extreme conditions. I was very surprised when I when I got the message "Unknown Exception" instead of "my message". Took me a couple of minutes in total astonishment to figure out what I was doing wrong. The fix is very simple, and is the accurate way to do re-throws in C++. I think I missed this detail due to the fact that I've been doing some C# lately, where this pattern is the way to do it (excluding the exception-chaining pattern). ps. This piece of code is a simplified version of my real code. I'm really not this stupid, as the code might indicate. ;P ds. [edit]I forgot the public inheritance modifier. It turned out that I forgot it in "real" code as well. See my part 2 just above this post. Switching between C# and C++ does stuff with your brain... At least mine. :([/edit]

    -- Kein Mitleid Für Die Mehrheit

    modified on Tuesday, December 23, 2008 7:13 AM

    N T E Y T 5 Replies Last reply
    0
    • J Jorgen Sigvardsson

      class MyException : public std::exception {
      public:
      const char* what() const { // overrides std::exception::what()
      return "my message";
      }
      };

      ...

      try {
      try {
      throw MyException();
      } catch(std::exception& ex) {
      throw ex;
      }
      } catch(std::exception& ex) {
      std::cout << ex.what() << std::endl;
      }

      What is the printout? I found it by accident, as I was testing how some code I just wrote, works in extreme conditions. I was very surprised when I when I got the message "Unknown Exception" instead of "my message". Took me a couple of minutes in total astonishment to figure out what I was doing wrong. The fix is very simple, and is the accurate way to do re-throws in C++. I think I missed this detail due to the fact that I've been doing some C# lately, where this pattern is the way to do it (excluding the exception-chaining pattern). ps. This piece of code is a simplified version of my real code. I'm really not this stupid, as the code might indicate. ;P ds. [edit]I forgot the public inheritance modifier. It turned out that I forgot it in "real" code as well. See my part 2 just above this post. Switching between C# and C++ does stuff with your brain... At least mine. :([/edit]

      -- Kein Mitleid Für Die Mehrheit

      modified on Tuesday, December 23, 2008 7:13 AM

      N Offline
      N Offline
      Nemanja Trifunovic
      wrote on last edited by
      #2

      Throwing polymorphically[^]? In all fairness, I think this is a "bit" weird way of doing things. I am still not sure if I like exceptions in general and especially the C++ way of implementing them.

      Programming Blog utf8-cpp

      1 Reply Last reply
      0
      • J Jorgen Sigvardsson

        class MyException : public std::exception {
        public:
        const char* what() const { // overrides std::exception::what()
        return "my message";
        }
        };

        ...

        try {
        try {
        throw MyException();
        } catch(std::exception& ex) {
        throw ex;
        }
        } catch(std::exception& ex) {
        std::cout << ex.what() << std::endl;
        }

        What is the printout? I found it by accident, as I was testing how some code I just wrote, works in extreme conditions. I was very surprised when I when I got the message "Unknown Exception" instead of "my message". Took me a couple of minutes in total astonishment to figure out what I was doing wrong. The fix is very simple, and is the accurate way to do re-throws in C++. I think I missed this detail due to the fact that I've been doing some C# lately, where this pattern is the way to do it (excluding the exception-chaining pattern). ps. This piece of code is a simplified version of my real code. I'm really not this stupid, as the code might indicate. ;P ds. [edit]I forgot the public inheritance modifier. It turned out that I forgot it in "real" code as well. See my part 2 just above this post. Switching between C# and C++ does stuff with your brain... At least mine. :([/edit]

        -- Kein Mitleid Für Die Mehrheit

        modified on Tuesday, December 23, 2008 7:13 AM

        T Offline
        T Offline
        TheGreatAndPowerfulOz
        wrote on last edited by
        #3

        The reason is that C++ uses compile-time type information (the "static" type) while C# uses runtime type information since metadata is built-in to C#. Hence the "throw ex" is actually throwing a std::exception& even though it "caught" a MyException&. The simple fix here is probably to use "throw" instead of "throw ex". Using "throw ex" is bad form anyway when rethrowing a caught exception. The reason is that the exception's stack trace is rooted to the new location of the throw instead of the original location.

        1 Reply Last reply
        0
        • J Jorgen Sigvardsson

          class MyException : public std::exception {
          public:
          const char* what() const { // overrides std::exception::what()
          return "my message";
          }
          };

          ...

          try {
          try {
          throw MyException();
          } catch(std::exception& ex) {
          throw ex;
          }
          } catch(std::exception& ex) {
          std::cout << ex.what() << std::endl;
          }

          What is the printout? I found it by accident, as I was testing how some code I just wrote, works in extreme conditions. I was very surprised when I when I got the message "Unknown Exception" instead of "my message". Took me a couple of minutes in total astonishment to figure out what I was doing wrong. The fix is very simple, and is the accurate way to do re-throws in C++. I think I missed this detail due to the fact that I've been doing some C# lately, where this pattern is the way to do it (excluding the exception-chaining pattern). ps. This piece of code is a simplified version of my real code. I'm really not this stupid, as the code might indicate. ;P ds. [edit]I forgot the public inheritance modifier. It turned out that I forgot it in "real" code as well. See my part 2 just above this post. Switching between C# and C++ does stuff with your brain... At least mine. :([/edit]

          -- Kein Mitleid Für Die Mehrheit

          modified on Tuesday, December 23, 2008 7:13 AM

          E Offline
          E Offline
          Eytukan
          wrote on last edited by
          #4

          Not very similar, but it's involving dotnet & C++. Once, When I was starting to code mixed mode, I put the catch block like : try { //Explosives } catch(System::Exception^ ex) { //Only managed clean up code. } catch(...) { //All unmanaged clean up code } And wondered why my module was leaking memory in tons.


          OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]

          1 Reply Last reply
          0
          • J Jorgen Sigvardsson

            class MyException : public std::exception {
            public:
            const char* what() const { // overrides std::exception::what()
            return "my message";
            }
            };

            ...

            try {
            try {
            throw MyException();
            } catch(std::exception& ex) {
            throw ex;
            }
            } catch(std::exception& ex) {
            std::cout << ex.what() << std::endl;
            }

            What is the printout? I found it by accident, as I was testing how some code I just wrote, works in extreme conditions. I was very surprised when I when I got the message "Unknown Exception" instead of "my message". Took me a couple of minutes in total astonishment to figure out what I was doing wrong. The fix is very simple, and is the accurate way to do re-throws in C++. I think I missed this detail due to the fact that I've been doing some C# lately, where this pattern is the way to do it (excluding the exception-chaining pattern). ps. This piece of code is a simplified version of my real code. I'm really not this stupid, as the code might indicate. ;P ds. [edit]I forgot the public inheritance modifier. It turned out that I forgot it in "real" code as well. See my part 2 just above this post. Switching between C# and C++ does stuff with your brain... At least mine. :([/edit]

            -- Kein Mitleid Für Die Mehrheit

            modified on Tuesday, December 23, 2008 7:13 AM

            Y Offline
            Y Offline
            Yuriy Levytskyy
            wrote on last edited by
            #5

            Hello! I'm very confused by your code. Your exception uses private std::exception as it's private parent. So MyException cannot be casted to std::exception. The code bellow works fine: #include #include namespace { class My_exception : public std::exception { public: virtual const char *what() const { return "My exception"; } }; } int main(int argc, char *argv[]) { try { try { throw My_exception(); } catch (std::exception &exception) { throw; } } catch (std::exception &exception) { std::cout << exception.what() << std::endl; } return 0; }

            J 1 Reply Last reply
            0
            • Y Yuriy Levytskyy

              Hello! I'm very confused by your code. Your exception uses private std::exception as it's private parent. So MyException cannot be casted to std::exception. The code bellow works fine: #include #include namespace { class My_exception : public std::exception { public: virtual const char *what() const { return "My exception"; } }; } int main(int argc, char *argv[]) { try { try { throw My_exception(); } catch (std::exception &exception) { throw; } } catch (std::exception &exception) { std::cout << exception.what() << std::endl; } return 0; }

              J Offline
              J Offline
              Jorgen Sigvardsson
              wrote on last edited by
              #6

              I forgot the public inheritance modifier when I wrote the sample code. I also have a part 2 just above this post. I've made the error of omitting the public modifier for real, and it can become very confusing indeed. Normally, I don't forget it, but since I'm switching back and forth between C# and C++, my brain gets confused (apparently). In C# all inheritances are public, so you don't specify anything - it looks just like a private inheritance in C++...

              -- Kein Mitleid Für Die Mehrheit

              1 Reply Last reply
              0
              • J Jorgen Sigvardsson

                class MyException : public std::exception {
                public:
                const char* what() const { // overrides std::exception::what()
                return "my message";
                }
                };

                ...

                try {
                try {
                throw MyException();
                } catch(std::exception& ex) {
                throw ex;
                }
                } catch(std::exception& ex) {
                std::cout << ex.what() << std::endl;
                }

                What is the printout? I found it by accident, as I was testing how some code I just wrote, works in extreme conditions. I was very surprised when I when I got the message "Unknown Exception" instead of "my message". Took me a couple of minutes in total astonishment to figure out what I was doing wrong. The fix is very simple, and is the accurate way to do re-throws in C++. I think I missed this detail due to the fact that I've been doing some C# lately, where this pattern is the way to do it (excluding the exception-chaining pattern). ps. This piece of code is a simplified version of my real code. I'm really not this stupid, as the code might indicate. ;P ds. [edit]I forgot the public inheritance modifier. It turned out that I forgot it in "real" code as well. See my part 2 just above this post. Switching between C# and C++ does stuff with your brain... At least mine. :([/edit]

                -- Kein Mitleid Für Die Mehrheit

                modified on Tuesday, December 23, 2008 7:13 AM

                T Offline
                T Offline
                Tad McClellan
                wrote on last edited by
                #7

                Ha, I've been doing vb.net for the last 5 years. My brain is now as rotten as they come. I wouldn't know a curly bracket if it hit me in the face anymore.

                TadMcClellan.Com

                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