Salome HOME
68abb4c46d16c4aeebbe7fb1d898f151413e248a
[modules/gui.git] / src / SalomeApp / SalomeApp_ExceptionHandler.cxx
1 #include "SalomeApp_ExceptionHandler.h"
2
3 #include <OSD.hxx>
4
5 #include <Standard_Failure.hxx>
6 #include <Standard_ErrorHandler.hxx>
7
8 #include <stdexcept>
9 #include <exception>
10
11 #include <qstring.h>
12
13 #include <CASCatch_CatchSignals.hxx>
14 #include <CASCatch_ErrorHandler.hxx>
15 #include <CASCatch_Failure.hxx> 
16
17
18 /*!Constructor. Initialize by \a floatSignal.*/
19 SalomeApp_ExceptionHandler::SalomeApp_ExceptionHandler( const bool floatSignal )
20 : SUIT_ExceptionHandler()
21 {
22   OSD::SetSignal( floatSignal );
23 }
24
25 /*!Try to call SUIT_ExceptionHandler::internalHandle(o, e), catch if failure.*/
26 bool SalomeApp_ExceptionHandler::handleSignals( QObject* o, QEvent* e )
27 {
28
29   CASCatch_CatchSignals aCatchSignals;
30   aCatchSignals.Activate();
31     
32     
33   CASCatch_TRY {   
34     SUIT_ExceptionHandler::internalHandle( o, e );
35   }
36   CASCatch_CATCH(CASCatch_Failure) {
37     aCatchSignals.Deactivate();
38     Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();          
39     throw std::runtime_error( aFail->GetError() );
40   }
41   
42   aCatchSignals.Deactivate();   
43   return true;
44 }
45
46 #ifdef try
47 #undef try
48 #endif
49
50 #ifdef catch
51 #undef catch
52 #endif
53
54 /*!Try to call handleSignals( o, e ), catch and show error message.*/
55 bool SalomeApp_ExceptionHandler::handle( QObject* o, QEvent* e )
56 {
57   bool res = false;
58   QString title( "Fatal error" );
59
60   try {
61     res = handleSignals( o, e );
62   }
63   catch( std::exception& ex )
64   {
65     showMessage( title, QString( ex.what() ) );
66   }
67   catch( Standard_Failure& e )
68   {
69     showMessage( title, QString( e.GetMessageString() ) );
70   }
71 #ifndef WNT
72   catch(...)
73   {
74     showMessage( title, "Unknown Exception" );
75   }
76 #endif
77
78   return res;
79 }
80
81 /*!Create new SUIT_ExceptionHandler*/
82 extern "C" SALOMEAPP_EXPORT SUIT_ExceptionHandler* getExceptionHandler()
83 {
84   return new SalomeApp_ExceptionHandler( true );
85 }