CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2005
    Posts
    445

    ::CloseHandle() throw an exception

    Hi Guys,

    Quote Originally Posted by msdn
    CloseHandle()
    If the function succeeds, the return value is nonzero.

    If the function fails, the return value is zero. To get extended error information, call GetLastError.

    If the application is running under a debugger, the function will throw an exception if it receives either a handle value that is not valid or a pseudo-handle value. This can happen if you close a handle twice, or if you call CloseHandle on a handle returned by the FindFirstFile function instead of calling the FindClose function.
    I want to catch the exception but I couldn't find what type it is.
    I've tried:

    Code:
    if ( m_hEvent != NULL && m_hEvent != INVALID_HANDLE_VALUE )
    {
        try
        {
    	::CloseHandle(m_hEvent);
    	::CloseHandle(m_hEvent); // Call it again to trigger an exception
        }
    			
        catch (const std::exception &e)			
        {				
             AfxMessageBox(_T("This is an exception"));
        }
       
        m_hEvent = NULL;
    }
    It didn't catch it.
    Can anyone tell me what type of exception is thrown?
    Do I need to use try catch or checking the handle against null will be enough?


    Many thanks
    Last edited by Salvadoravi; December 12th, 2008 at 01:05 PM.

  2. #2
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: ::CloseHandle throw an exception

    There are no exception: use GetLastError to know what is happening.

    Code:
    ::CloseHandle(m_hEvent);
    DWORD dwError = GetLastError();
    And chek the value of dwError.



    Regards !!

  3. #3
    Join Date
    Jan 2008
    Posts
    178

    Re: ::CloseHandle throw an exception

    Quote Originally Posted by juanpast View Post
    There are no exception
    Of course there is an exception !
    See the source code of CloseHandle()...

  4. #4
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: ::CloseHandle throw an exception

    http://msdn.microsoft.com/en-us/library/aa914720.aspx


    I can not read anything about exception...

  5. #5
    Join Date
    Sep 2005
    Location
    London
    Posts
    208

    Re: ::CloseHandle throw an exception

    Quote Originally Posted by juanpast View Post
    http://msdn.microsoft.com/en-us/library/aa914720.aspx
    I can not read anything about exception...
    Under a debugger CloseHandle() WILL throw an exception when the handle is invalid.

    juanpast, you are looking at the Windows CE / Windows Mobile platform.
    check: http://msdn.microsoft.com/en-us/libr...11(VS.85).aspx *-)

    Quote Originally Posted by salvadoravi
    Do I need to use try catch
    No

    Quote Originally Posted by salvadoravi
    or checking the handle against null will be enough?
    That would deal with the handle validation side of things but it won't prevent from an exception to be thrown if you call CloseHandle() twice.

    Code:
    ~CSPEvent()
    {
        ::CloseHandle(m_hEvent);  
    
       // The HANDLE is now invalid
    		
        if ( m_hEvent != NULL && m_hEvent != INVALID_HANDLE_VALUE )
        {			
            // That will throw an exception
            ::CloseHandle(m_hEvent); 
            m_hEvent = NULL;
        }
    }
    Hope it helps
    Doron Moraz



    EDIT: According to MSDN the function will also throw an exception if you call CloseHandle on a handle returned by the FindFirstFile function instead of calling the FindClose function.
    Last edited by Doron Moraz; December 15th, 2008 at 05:53 AM.

  6. #6
    Join Date
    Dec 2005
    Posts
    445

    Re: ::CloseHandle throw an exception

    Thank you !

  7. #7
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: ::CloseHandle throw an exception

    Thanks Doron !!!

    Sorry to all, I am in a mistake....





    Regards !!

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