?
Solved

A problem about SEH and C++ Exception handle

Posted on 2001-07-22
3
Medium Priority
?
847 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 300 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.
Suggested Courses

752 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