Exception handling problem.
-
What am I doing? I want my template class to send an exception when one of input parameters is out of range. How am I doing that? The main parts of the source code are presented below. Does it work? Yes, it does. The exception is being sent and caught in the program passing wrong input parameters, error message is successfully displayed. So, what is wrong? I receive a strange warning during compilation: Warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow) I don't know what's wrong with it, but I don't like warnings... especially those that I don't understand. If you can tell me what the compliler (MS VC .NET) wants from me I will be very grateful to you. Thank you!!! #define CLogicError std::logic_error #define STRING std::string class CMyError : public CLogicError { public: CMyError(const STRING& what_arg) : CLogicError(what_arg) {}; }; template class TMy { ... T& operator()(const int nRow, const int nCol) throw (CMyError); ... } template T& TMy::operator()(const int nRow, const int nCol) throw (CMyError) { ... } The warning points to the line T& operator()(const int nRow, const int nCol) throw (CMyError); in class definition. It does not seem to like "throw (CMyError)" part. If I change it to "throw()" the compiler stops complaining.
-
What am I doing? I want my template class to send an exception when one of input parameters is out of range. How am I doing that? The main parts of the source code are presented below. Does it work? Yes, it does. The exception is being sent and caught in the program passing wrong input parameters, error message is successfully displayed. So, what is wrong? I receive a strange warning during compilation: Warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow) I don't know what's wrong with it, but I don't like warnings... especially those that I don't understand. If you can tell me what the compliler (MS VC .NET) wants from me I will be very grateful to you. Thank you!!! #define CLogicError std::logic_error #define STRING std::string class CMyError : public CLogicError { public: CMyError(const STRING& what_arg) : CLogicError(what_arg) {}; }; template class TMy { ... T& operator()(const int nRow, const int nCol) throw (CMyError); ... } template T& TMy::operator()(const int nRow, const int nCol) throw (CMyError) { ... } The warning points to the line T& operator()(const int nRow, const int nCol) throw (CMyError); in class definition. It does not seem to like "throw (CMyError)" part. If I change it to "throw()" the compiler stops complaining.
It simply means that the compiler doesn't support exception specifications (semantically). That is, the compiler does not generate code to enforce the rule that the function can't throw *any* exception other than
CMyError
. You can try it out, try throwing any other exception, it will still be caught in the corresponding catch block, but according to the standard, it must result in a call tounexpected
and then aborting of the program.throw()
means that your function is not allowed to throw exceptions at all, I don't know why that doesn't result in a warning? May be VC++.NET supports just that special case? Regards Senthil _____________________________ My Blog | My Articles | WinMacro -
It simply means that the compiler doesn't support exception specifications (semantically). That is, the compiler does not generate code to enforce the rule that the function can't throw *any* exception other than
CMyError
. You can try it out, try throwing any other exception, it will still be caught in the corresponding catch block, but according to the standard, it must result in a call tounexpected
and then aborting of the program.throw()
means that your function is not allowed to throw exceptions at all, I don't know why that doesn't result in a warning? May be VC++.NET supports just that special case? Regards Senthil _____________________________ My Blog | My Articles | WinMacroHi Senthil, Thank you for the response.
throw()
does not lead to any warnings at all. I tried as you suggested and in fact caught other exception as well. The debugger still reported about an "unhandled exception" though but did not abort my program. When I throw the correct type of exception the debugger does not report about an "unhandled exception". So, I assume you are right about the compiler not enforcing the exception type upon the function that throws exceptions. Since it is actually one of the best compilers in the world, I am a little bit skeptical that it is a compiler's fault though. I suspect and, in fact, am more inclined to think that I have done something wrong. Maybe there is an option that I have to set to force the implementation of this exception restriction upon functions? I have not found any in the project settings that has anything to do with exceptions. Or else, may it have anything to do with templates, since they are implemented in *.h file rather than in *.cpp? -
Hi Senthil, Thank you for the response.
throw()
does not lead to any warnings at all. I tried as you suggested and in fact caught other exception as well. The debugger still reported about an "unhandled exception" though but did not abort my program. When I throw the correct type of exception the debugger does not report about an "unhandled exception". So, I assume you are right about the compiler not enforcing the exception type upon the function that throws exceptions. Since it is actually one of the best compilers in the world, I am a little bit skeptical that it is a compiler's fault though. I suspect and, in fact, am more inclined to think that I have done something wrong. Maybe there is an option that I have to set to force the implementation of this exception restriction upon functions? I have not found any in the project settings that has anything to do with exceptions. Or else, may it have anything to do with templates, since they are implemented in *.h file rather than in *.cpp?Since it is actually one of the best compilers in the world, I am a little bit skeptical that it is a compiler's fault though. I suspect and, in fact, am more inclined to think that I have done something wrong. No, you haven't done anything wrong. MSVC.NET does not support exception specifications except
throw()
, as the compiler itself and Kumar point out. For what is worth, exception specification has become in recent years to be regarded as a design flaw in C++, so you might consider not using it. Follow this link[^] for some explanations about why exception specification is probably best avoided. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]! -
Since it is actually one of the best compilers in the world, I am a little bit skeptical that it is a compiler's fault though. I suspect and, in fact, am more inclined to think that I have done something wrong. No, you haven't done anything wrong. MSVC.NET does not support exception specifications except
throw()
, as the compiler itself and Kumar point out. For what is worth, exception specification has become in recent years to be regarded as a design flaw in C++, so you might consider not using it. Follow this link[^] for some explanations about why exception specification is probably best avoided. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!Thank you, Joaquín. It is pretty unfortunate that an ISO standard is not supported but I am going to follow your suggestion and avoid exception specifications. Thanks again!