Solved

A problem about SEH and C++ Exception handle

Posted on 2001-07-22
3
832 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

726 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