Solved

A problem about SEH and C++ Exception handle

Posted on 2001-07-22
3
838 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 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