C# rots your C++ brain [modified]
-
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
-
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
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.
-
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
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.
-
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
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[^]
-
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
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; }
-
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; }
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
-
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
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.