May 31, 2011

Anticross Anticross
Hobby Entomologist
248 posts

How to catch unhanded exception ?

 

I’ve got QCoreApplication. I run it at Microsoft Windows OS and need to catch unhanded exception. I try to do this:

  1. try{
  2.   m_defMail.setDefault();
  3. #ifdef _DEBUG
  4.   m_defMail.setDebugDefault();
  5. #endif
  6.  
  7. //   int g = 0;
  8. //   int t = g/g;
  9.  
  10.   initPathes();
  11.   initArgs();
  12.   analyseArguments(argc, argv);
  13.   sendAllMessages();
  14.  }
  15.  catch (...){
  16.   qDebug() << "sxMailSender: Unhanded Exception!!! Application will be terminated.";
  17.  }--

but it’s have no effect.

Edit: moved to C++, as it is no Qt related topic. Gerolf

6 replies

May 31, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

which exception do you try to catch?
There are two types:

  • “normal” exception (by throw) which can be caught by try/catch
  • structured exceptions (like div 0, null pointer, etc.) which can’t be catched by try/catch. For those you need try/except which is not possible in functions using C++ objects :-( Then you need an exception translator (_set_se_translator). There you can convert the structured exception into a C++ exception and throw that one. But sorry,. I can’t give you code for that, as it is closed source what I do here.
 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

May 31, 2011

Anticross Anticross
Hobby Entomologist
248 posts

  1. try{
  2.   m_defMail.setDefault();
  3. #ifdef _DEBUG
  4.   m_defMail.setDebugDefault();
  5. #endif
  6.  
  7.    int g = 0;
  8.    int t = g/g;
  9.  
  10.   initPathes();
  11.   initArgs();
  12.   analyseArguments(argc, argv);
  13.   sendAllMessages();
  14.  }
  15.  catch (...){
  16.   qDebug() << "sxMailSender: Unhanded Exception!!! Application will be terminated.";
  17.  }--

I mean for example in this source i try to catch div by zero. But I can’t reach catch section and get OS exception message. So I need some how to catch all OS exceptions for my application.

May 31, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

Did you read my post completely?

structured exceptions (like div 0, null pointer, etc.) which can’t be catched by try/catch.

Therefore you need structured exception handling or exception translation.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

May 31, 2011

Maxim Prishchepa Maxim Prishchepa
Lab Rat
96 posts

Also you can use flag /EHa if you use compiler from Microsoft (description this flag you can find in MSDN [msdn.microsoft.com]).aspx

 Signature 

Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz).

May 31, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

/EHa does enable structured exception handling. It does not enable try/catch for structured exceptions.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

May 31, 2011

jdarnold jdarnold
Lab Rat
37 posts

I use the Windows ::SetUnhandledExceptionFilter API:

  1. ....
  2.     ::SetUnhandledExceptionFilter( IGExceptionHandler );
  3. ...
  4.  
  5. #if defined(WIN32)
  6. LONG WINAPI IGExceptionHandler( struct _EXCEPTION_POINTERS *pExceptionInfo )
  7. {
  8.     DWORD code = pExceptionInfo->ExceptionRecord->ExceptionCode;
  9.     IGLog( qDebug(), "Handling Visimeet Exception : " << code << " Address: " << pExceptionInfo->ExceptionRecord->ExceptionAddress);
  10.     if ( code == STATUS_ACCESS_VIOLATION )
  11.     { // add this bit o' info
  12.         IGLog( qDebug(), "**** ACCESS VIOLATION ****")
  13.     }
  14.     else if ( code == STATUS_ILLEGAL_INSTRUCTION )
  15.     {
  16.         IGLog( qDebug(), "**** ILLEGAL INSTRUCTION ****");
  17.     }
  18.     else if ( code == STATUS_FLOAT_DIVIDE_BY_ZERO || code == STATUS_INTEGER_DIVIDE_BY_ZERO )
  19.     {
  20.         IGLog( qDebug(), "**** DIVIDE BY ZERO ****");
  21.     }
  22.     else if ( code == IGLOGGER_FATAL_ERROR )
  23.     { // our interanl error if qFatal is used
  24.         IGLog( qDebug(), "**** Qt FATAL ERROR ****")
  25.     }
  26.     else
  27.     {
  28.         IGLog( qDebug(), "**** Unknown Fatal Error ****");
  29.     }
  30.  
  31.     LONG retval = EXCEPTION_EXECUTE_HANDLER;
  32.  
  33.     visiqt->sendReport(true);
  34.  
  35.     return retval;
  36. }

 
  ‹‹ static cast other then int, char and boolean not possible?      [SOLVED] Double to Int ››

You must log in to post a reply. Not a member yet? Register here!