Solved

A problem about SEH and C++ Exception handle

Posted on 2001-07-22
3
799 Views
Last Modified: 2012-05-04
I write a function in a class.But when i compile,the VC++ compiler show me
"error C2712: Cannot use __try in functions that require object unwinding".
It seems that the "throw" can't be used with __try,is that true and why?
Could you tell me
what is "object unwinding" and what are the differences between "object unwinding" and "global unwinding"?Thanks

BOOL CThreadPool::Run(LPTHREADPROC proc,void *pParam){
     if (proc==NULL)
          throw std::invalid_argument(TEXT("Thread function can't be null"));
     
     __try{
          WaitForSingleObject(m_hMutex,INFINITE);
                .
                .
                .
          return FALSE;
     }
     __finally{
          ReleaseMutex(m_hMutex);
     }
     return TRUE;
}
0
Comment
Question by:jatom
3 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 6306021
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 6306042
>> It seems that the "throw" can't be used with __try,is that true and why?
VC supports 2 bascially unrelated exception mechanisms.  

First there is SEH (structured exception handling).  This mechanism is built into windows and is not langauge depdant.  i.e. it can be used in C or Pascal ot other langauges in windows programs.   since this is language independant, you can't expect it to handle language specifc issues--like calling destructors.  The Windows OS doesn't know how to handle calling destructors during a stack unwind.  That would depend upon not only the program's lanuage, but also the exact compiler used to create the program.  So in SEH destrctors are not called during stack unwind and there may be other problems as well with other langauge-specific features.

The second exception mechanism is ordianry C++ exceptions.  There are part of the C++ language. and so they do not relly on the OS in any way.  Since they are part of the lanague, they can do language-specific operations, like calling destructors.

If you are writting in C++, its probably best to use C++ exceptions (try...catch) not SEH exceptions (_try...__except) for all your exceptions.   (Al the ones you generate) since they are much more compatible with C++ and are much more portable to other OSs.   If you must use SEH (since the OS uses SEH when it detects a problem, The OS can't throuw a C++ exception since that is langauge-specific), then you should probably convert the structured exception into a C++ exception.   You can do this by either carchign the SE right away and then throwing a C++ exception.  Or by using _set_se_translator().  This sets a function that is called each time a SE occurs.  You can set this to be a function that then throws a C++ exception.  i.e. you write a functionm that converts the SE to a C++ exception.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6306470
The simplest solution is separating both mechanisms in two different functions:

BOOL CThreadPool::Run(LPTHREADPROC proc,void *pParam){
    if (proc==NULL)
         throw std::invalid_argument(TEXT("Thread function can't be null"));

    MutexWait ( m_hMutex, INFINITE);    

    return TRUE;
}

void MutexWait ( HANDLE hMutex, DWORD dwTimeout)
{
    __try{
         WaitForSingleObject(hMutex,dwTimeout);
               .
               .
               .
         return FALSE;
    }
    __finally{
         ReleaseMutex(hMutex);
    }
}

so you'll get rid of the problem.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now