Salome HOME
Porting KERNEL on new XML reader.
[modules/kernel.git] / src / CASCatch / CASCatch_ErrorHandler.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #ifdef NO_CXX_EXCEPTION
22
23 #include "CASCatch_ErrorHandler.hxx"
24
25 // During setjmp()/longjmp() K_SETJMP_CASCatch is non zero (try)
26 // So if there is an abort request and if K_SETJMP_CASCatch is non zero, the abort
27 // request will be ignored. If the abort request do a raise during a setjmp
28 // or a longjmp, there will be a "terminating SEGV" impossible to handle.
29
30
31 Standard_EXPORT int K_SETJMP_CASCatch = 0 ; 
32
33 static Handle(CASCatch_Failure) GlbError;  //Last caught Error, Null if there is no error
34
35 static CASCatch_ErrorHandler*   Top = 0;   //The top of the Errors Stack
36
37 //=======================================================================
38 //function : CASCatch_ErrorHandler
39 //purpose  : Constructor
40 //=======================================================================
41 CASCatch_ErrorHandler::CASCatch_ErrorHandler ()
42 {
43   Previous = Top;
44   Top      = this;
45   CaughtError.Nullify();
46   GlbError.Nullify();
47 }
48
49 //=======================================================================
50 //function : ~CASCatch_ErrorHandler
51 //purpose  : Destructor : Delete the ErrorHandler and Abort if there is a 'Error'.
52 //=======================================================================
53 CASCatch_ErrorHandler::~CASCatch_ErrorHandler()
54 {
55   Top = Top->Previous;
56   if( !GlbError.IsNull() ) Abort();
57 }
58
59 //=======================================================================
60 //function : Abort: make a longjmp to the saved Context.
61 //purpose  : Abort if there is a non null 'Error'
62 //=======================================================================
63 void CASCatch_ErrorHandler::Abort ()
64 {
65   //==== Check if can do the "longjmp" =======================================
66   if(Top == NULL || Top->Label == NULL) {
67     cout << "*** Abort *** an exception was raised, but no catch was found." << endl;
68     cout << "\t... The exception is:" << GlbError;
69     exit(1);
70   }
71
72 #ifdef DO_ABORT
73   if ( K_SETJMP_CASCatch )
74     cout << "Recursive abort ===> Terminating SEGV ..." << endl ;
75   K_SETJMP_CASCatch = 1 ;
76 #endif
77
78   longjmp(Top->Label, Standard_True);
79 }
80
81 //=======================================================================
82 //function : Catches
83 //purpose  : If there is a 'Error', and it is in good type 
84 //           returns True and clean 'Error', else returns False.
85 //=======================================================================
86 Standard_Boolean CASCatch_ErrorHandler::Catches 
87   (const Handle(Standard_Type)& AType) 
88 {
89 #ifdef DO_ABORT
90   K_SETJMP_CASCatch = 0 ;
91 #endif
92   if(GlbError.IsNull())
93     return Standard_False;
94
95   if(GlbError->IsKind(AType)){
96     CaughtError = GlbError;
97     GlbError.Nullify();
98     return Standard_True;
99   } else {
100     return Standard_False;
101   }
102 }
103
104 //=======================================================================
105 //function : LastCaughtError
106 //purpose  : 
107 //=======================================================================
108 Handle(CASCatch_Failure) CASCatch_ErrorHandler::LastCaughtError()
109 {
110   return Top->CaughtError;
111 }
112
113 //=======================================================================
114 //function : Error
115 //purpose  : 
116 //=======================================================================
117 void CASCatch_ErrorHandler::Error(const Handle(CASCatch_Failure)& aError)
118 {
119   GlbError = aError;
120 }
121
122 #endif