Salome HOME
Copyrights update 2015.
[modules/gui.git] / src / SalomeApp / SalomeApp_ExceptionHandler.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "SalomeApp_ExceptionHandler.h"
24 #include "Utils_CorbaException.hxx"
25
26 #include "CASCatch.hxx"
27
28 #include <OSD.hxx>
29
30 #include <stdexcept>
31 #include <exception>
32
33 #include <QString>
34
35 #include <Standard_ErrorHandler.hxx>
36 #include <Standard_Failure.hxx>
37
38
39 /*!Constructor. Initialize by \a floatSignal.*/
40 SalomeApp_ExceptionHandler::SalomeApp_ExceptionHandler( const bool floatSignal )
41 : SUIT_ExceptionHandler()
42 {
43   // JFA 2006-09-28: PAL10867: suppress signal catching,
44   // if environment variable DISABLE_SIGNALS_CATCHING is set to 1.
45   // Commonly this is used with "noexcepthandler" option.
46   char* envNoCatchSignals = getenv("NOT_INTERCEPT_SIGNALS");
47   if (!envNoCatchSignals || !atoi(envNoCatchSignals))
48   {
49     OSD::SetSignal( floatSignal );
50   }
51 }
52
53 /*!Try to call SUIT_ExceptionHandler::internalHandle(o, e), catch if failure.*/
54 bool SalomeApp_ExceptionHandler::handleSignals( QObject* o, QEvent* e )
55 {
56   try {
57     OCC_CATCH_SIGNALS;
58     SUIT_ExceptionHandler::internalHandle( o, e );
59   }
60   catch(Standard_Failure) {
61     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
62     throw Standard_Failure( aFail->GetMessageString() );
63   }
64
65   return true;
66 }
67
68 /*!Try to call handleSignals( o, e ), catch and show error message.*/
69 bool SalomeApp_ExceptionHandler::handle( QObject* o, QEvent* e )
70 {
71   bool res = false;
72   QString title( "Fatal error" );
73
74   try {
75     res = handleSignals( o, e );
76   }
77   catch( std::exception& ex )
78   {
79     showMessage( title, QString( ex.what() ) );
80   }
81   catch( Standard_Failure& e )
82   {
83     showMessage( title, QString( e.GetMessageString() ) );
84   }
85   catch( SALOME::SALOME_Exception& ex)
86   {
87     showMessage( title, QString( ex.details.text));
88   }
89 #ifndef WIN32
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   // MSV 2006-04-26: work around PAL12004 "VTK window => SIGFPE Arithmetic Exception Detected"
103   // We disable FPE signal as it was in earlier versions of SALOME. It is enabled
104   // only in debug mode if the environment variable DISABLE_FPE is not set to 1.
105   bool raiseFPE;
106   raiseFPE = false;
107
108 #if defined(_DEBUG_) | defined(_DEBUG) //the Last for WIN32 default settings
109   char* envEnableFPE = getenv("ENABLE_FPE");
110   if (envEnableFPE && atoi(envEnableFPE))
111     raiseFPE = true;
112 #endif
113
114   return new SalomeApp_ExceptionHandler( raiseFPE );
115 }