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. Exception handling problem.

Exception handling problem.

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestioncsharpc++
5 Posts 3 Posters 1 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.
  • N Offline
    N Offline
    Nick_Kisialiou
    wrote on last edited by
    #1

    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.

    S 1 Reply Last reply
    0
    • N Nick_Kisialiou

      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.

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      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 to unexpected 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

      N 1 Reply Last reply
      0
      • S S Senthil Kumar

        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 to unexpected 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

        N Offline
        N Offline
        Nick_Kisialiou
        wrote on last edited by
        #3

        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?

        J 1 Reply Last reply
        0
        • N Nick_Kisialiou

          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?

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          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[^]!

          N 1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            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[^]!

            N Offline
            N Offline
            Nick_Kisialiou
            wrote on last edited by
            #5

            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!

            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