CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Exceptions

  1. #1
    Join Date
    Jun 2002
    Posts
    1,417

    Exceptions

    If the Registry class shown below throws an exception, I get Windows error "Unhandled Exception.". I though my catch block would get executed.

    Code:
    class CRegistry
    {
    public:
       CRegistry()
       {
    	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, base, 0, KEY_ALL_ACCESS, &hkey_) != ERROR_SUCCESS)
    	{
    		// Can't open the registry key, so throw an exception.
    		throw;
    		// Lines beyone here are never executed.  
    	}
      }
    
    // remainder of class not shown
    };
    
    
    
    
    int main()
    {
      try
      {
    	Registry reg("Software\\COE\\Software\\GTSAIT");
      }
      catch(...)
      {
    	AfxMessageBox("Exception caught");
      }
    }

  2. #2
    Join Date
    Jan 2004
    Posts
    20

    Re: Exceptions

    Originally posted by stober
    If the Registry class shown below throws an exception, I get Windows error "Unhandled Exception.". I though my catch block would get executed.
    from the c++ standard, 15.1 (Throwing an exception):

    • 6. A throw-expression with no operand rethrows the exception being handled. The exception is reactivated with the existing temporary; no new temporary exception object is created. The exception is no longer considered to be caught; therefore, the value of uncaught_exception() will again be true.

      8. If no exception is presently being handled, executing a throw-expression with no operand calls terminate()


    so apparently you were wrong.

  3. #3
    Join Date
    Jun 2002
    Posts
    1,417
    Yes, I know I'm wrong -- but how can I catch that exception ?

  4. #4
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    you don't, re read 8. the best you can do is installing an SetUnhandledExceptionFilter and continuing the execution

    or throw this and catch a CRegistry

  5. #5
    Join Date
    Dec 2003
    Posts
    220
    Originally posted by Mick
    you don't, re read 8. the best you can do is installing an SetUnhandledExceptionFilter and continuing the execution
    Do you know how to install A SetUnhandledExceptionFilter ?
    May I ask ?

    Thanks,

    Regards,

    homestead

  6. #6
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Originally posted by Homestead
    Do you know how to install A SetUnhandledExceptionFilter ?
    May I ask ?

    Thanks,

    Regards,

    homestead
    why yes...do you know how to use MSDN may I ask?

  7. #7
    Join Date
    Dec 2003
    Posts
    220
    Originally posted by Mick may I ask?
    Yes, of course.
    do you know how to use MSDN
    I searched again and again but found nothing, I thought you knew how to install it though.
    Could you tell me now ?

    Regards,

    homestead

  8. #8
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    long __stdcall bar(EXCEPTION_POINTERS* t)
    {
    }

    SetUnhandledExceptionFilter(bar);

  9. #9
    Join Date
    Dec 2003
    Posts
    220
    Originally posted by Mick
    long __stdcall bar(EXCEPTION_POINTERS* t)
    {
    }

    SetUnhandledExceptionFilter(bar);
    Thanks a lot,

    Regards,

    homestead

  10. #10
    Join Date
    Jun 2002
    Posts
    1,417
    Originally posted by Mick
    or throw this and catch a CRegistry
    Thanks -- that's exactly what I wanted to know.

  11. #11
    Join Date
    Jan 2004
    Posts
    20
    Originally posted by Mick
    you don't, re read 8. the best you can do is installing an SetUnhandledExceptionFilter and continuing the execution

    or throw this and catch a CRegistry
    or install a terminate() handler with set_terminate()

    otherwise, just throw an exception object for catch to receive. however, throwing *this would be a serious mistake, as it's not completely constructed yet - there's no guarantee that you'll have it in any usable state to manipulate in the catch block. rather, use some class derived from a standard exception class, depending on your need. ex (on msvc might look different):

    Code:
    class MyExc : public std::exception
    {
        const char* s;
    public:
        MyExc(const char* s_) : s(s_){}
        virtual const char* what() const throw()
        {
            return s;
        }
    };
    
    ...
       try {
          ...
          throw MyExc("some error message");
          ...
       }
       catch(std::exception& e)
       {
          fprintf(stderr, "%s\n", e.what() );
       }

  12. #12
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Originally posted by abc_coder
    or install a terminate() handler with set_terminate()
    which terminates...different handler...

    otherwise, just throw an exception object for catch to receive. however, throwing *this would be a serious mistake, as it's not completely constructed yet -
    only in the ctor as indicated.

  13. #13
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    let me add it's perfectly legal to throw a exception in a ctor, it's just that you have to make sure you clean up any thing you have allocated.

    /that is all, move along now..

  14. #14
    Join Date
    Dec 2003
    Posts
    220
    Originally posted by Mick
    let me add it's perfectly legal to throw a exception in a ctor, it's just that you have to make sure you clean up any thing you have allocated.
    I just wanted to know why you said it is perfectly legal to throw AN exception in a ctor ? Where did you learn it ?

    May I ask ?

    Thanks a lot

    Regards,

    homestead

  15. #15
    Join Date
    Jan 2004
    Posts
    20
    Originally posted by Homestead
    I just wanted to know why you said it is perfectly legal to throw AN exception in a ctor ? Where did you learn it ?

    May I ask ?

    Thanks a lot

    Regards,

    homestead
    it's the intended behavior. Constructors don't return error codes so if you do a one-step (i.e. in the constructor) initialization of some resource and that fails, the most dirrect way of announcing the failure is by throwing an exception. (there are other less direct ways too, but you have to revert to C-style error checking)

    also, to handle the cleanup (if you're throwing from a constructor, *this is not completely constructed and thus the destructor won't be called), constructor initializers are handy: any subobjects will be in a definite state and will be handled nicely by the exception cleanup procedure.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured