Salome HOME
Copyrights update
[modules/gui.git] / src / SalomeApp / SalomeApp_ExceptionHandler.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include "SalomeApp_ExceptionHandler.h"
20
21 #include <OSD.hxx>
22
23 #include <Standard_Failure.hxx>
24 #include <Standard_ErrorHandler.hxx>
25
26 #include <stdexcept>
27 #include <exception>
28
29 #include <qstring.h>
30
31 #include <CASCatch_CatchSignals.hxx>
32 #include <CASCatch_ErrorHandler.hxx>
33 #include <CASCatch_Failure.hxx> 
34
35
36 /*!Constructor. Initialize by \a floatSignal.*/
37 SalomeApp_ExceptionHandler::SalomeApp_ExceptionHandler( const bool floatSignal )
38 : SUIT_ExceptionHandler()
39 {
40   OSD::SetSignal( floatSignal );
41 }
42
43 /*!Try to call SUIT_ExceptionHandler::internalHandle(o, e), catch if failure.*/
44 bool SalomeApp_ExceptionHandler::handleSignals( QObject* o, QEvent* e )
45 {
46
47   CASCatch_CatchSignals aCatchSignals;
48   aCatchSignals.Activate();
49     
50     
51   CASCatch_TRY {   
52     SUIT_ExceptionHandler::internalHandle( o, e );
53   }
54   CASCatch_CATCH(CASCatch_Failure) {
55     aCatchSignals.Deactivate();
56     Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();          
57     throw std::runtime_error( aFail->GetError() );
58   }
59   
60   aCatchSignals.Deactivate();   
61   return true;
62 }
63
64 #ifdef try
65 #undef try
66 #endif
67
68 #ifdef catch
69 #undef catch
70 #endif
71
72 /*!Try to call handleSignals( o, e ), catch and show error message.*/
73 bool SalomeApp_ExceptionHandler::handle( QObject* o, QEvent* e )
74 {
75   bool res = false;
76   QString title( "Fatal error" );
77
78   try {
79     res = handleSignals( o, e );
80   }
81   catch( std::exception& ex )
82   {
83     showMessage( title, QString( ex.what() ) );
84   }
85   catch( Standard_Failure& e )
86   {
87     showMessage( title, QString( e.GetMessageString() ) );
88   }
89 #ifndef WNT
90   catch(...)
91   {
92     showMessage( title, "Unknown Exception" );
93   }
94 #endif
95
96   return res;
97 }
98
99 /*!Create new SUIT_ExceptionHandler*/
100 extern "C" SALOMEAPP_EXPORT SUIT_ExceptionHandler* getExceptionHandler()
101 {
102   return new SalomeApp_ExceptionHandler( true );
103 }