-
December 12th, 2008, 12:51 PM
#1
::CloseHandle() throw an exception
Hi Guys,
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.
-
December 13th, 2008, 11:25 AM
#2
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 !!
-
December 13th, 2008, 09:13 PM
#3
Re: ::CloseHandle throw an exception
Originally Posted by juanpast
There are no exception
Of course there is an exception !
See the source code of CloseHandle()...
-
December 14th, 2008, 10:50 AM
#4
Re: ::CloseHandle throw an exception
-
December 14th, 2008, 12:39 PM
#5
Re: ::CloseHandle throw an exception
Originally Posted by juanpast
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 *-)
Originally Posted by salvadoravi
Do I need to use try catch
No
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.
-
December 14th, 2008, 12:41 PM
#6
Re: ::CloseHandle throw an exception
-
December 14th, 2008, 12:47 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|