Version: 9.12.0
SMESH_TryCatch.hxx
Go to the documentation of this file.
1 // Copyright (C) 2007-2023 CEA, EDF, 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 // File : SMESH_TryCatch.hxx
23 // Created : Mon Dec 17 15:43:38 2012
24 // Author : Edward AGAPOV (eap)
25 
26 #ifndef __SMESH_TryCatch_HXX__
27 #define __SMESH_TryCatch_HXX__
28 
29 #include "SMESH_Comment.hxx"
30 #include "SMESH_ComputeError.hxx"
31 #include "SMESH_Utils.hxx"
32 
33 #include <Utils_SALOME_Exception.hxx>
34 #include <Standard_Failure.hxx>
35 #include <Standard_ErrorHandler.hxx>
36 #include <utilities.h>
37 
38 // IMPORTANT: include this file _after_ OCC ones, else OCC_CATCH_SIGNALS can be undefined!
39 
40 #ifndef OCC_CATCH_SIGNALS
41 #define OCC_CATCH_SIGNALS
42 #endif
43 
44 // Define macros to catch and convert some of possible exceptions into text or SALOME_Exception.
45 // WARNING: SALOME::SALOME_Exception (CORBA exception) is not treated here; to care about it add
46 // #define SMY_OWN_CATCH catch ( SALOME::SALOME_Exception & e ) { do_something(e); }
47 // before #include<SMESH_TryCatch.hxx>
48 
49 
50 //-------------------------------------------------------------------------------------
51 #define SMESH_TRY \
52  try { \
53  OCC_CATCH_SIGNALS \
54 
55 //-------------------------------------------------------------------------------------
56 // A macro to add a custom catch clause to SMESH_CATCH
57 // To add your own catch close, define SMY_OWN_CATCH macro before including this file.
58 #ifndef SMY_OWN_CATCH
59 #define SMY_OWN_CATCH
60 #endif
61 
62 //-------------------------------------------------------------------------------------
63 // A macro allowing to retrieve a result returned by onExceptionFun
64 #define SMESH_CAUGHT
65 
66 //-------------------------------------------------------------------------------------
67 // A macro makes description of a caught exception and calls onExceptionFun(const char*).
68 // Several onExceptionFun() are defined here: throwSalomeEx(), doNothing() and returnError().
69 // To add your own catch clause, define SMY_OWN_CATCH macro before including this file.
70 
71 #define SMESH_CATCH( onExceptionFun ) \
72  } \
73  \
74  SMY_OWN_CATCH \
75  \
76  catch (Standard_Failure& ex) \
77  { \
78  SMESH_Comment text("OCCT Exception: "); \
79  text << ": " << ex.DynamicType()->Name(); \
80  if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) \
81  text << ": " << ex.GetMessageString(); \
82  SMESH_CAUGHT onExceptionFun( text ); \
83  } \
84  catch ( ::SMESH_ComputeError& ce ) \
85  { \
86  if ( !ce.myComment.empty() ) \
87  SMESH_CAUGHT onExceptionFun( ce.myComment.c_str() ); \
88  else if ( ce.IsCommon() ) \
89  SMESH_CAUGHT onExceptionFun( ce.CommonName().c_str() ); \
90  else \
91  SMESH_CAUGHT onExceptionFun \
92  (SMESH_Comment("SMESH_ComputeError: ") << ce.myName ); \
93  } \
94  catch ( const std::exception& ex) \
95  { \
96  SMESH_CAUGHT onExceptionFun( ex.what() ); \
97  } \
98  catch (...) \
99  { \
100  SMESH_CAUGHT onExceptionFun("Unknown Exception caught"); \
101  }
102 
103 //-------------------------------------------------------------------------------------
104 // Functions that can be used as an argument of SMESH_CATCH
105 
106 namespace SMESH
107 {
108  SMESHUtils_EXPORT void throwSalomeEx(const char* txt);
109  SMESHUtils_EXPORT void doNothing(const char* txt);
110  SMESHUtils_EXPORT const char* returnError(const char* txt);
111  SMESHUtils_EXPORT void printErrorInDebugMode(const char* txt);
112 }
113 
114 #endif
#define SMESHUtils_EXPORT
Definition: SMESH_Utils.hxx:37
Definition: SMESH_ControlsDef.hxx:64
void doNothing(const char *txt)
Definition: SMESH_TryCatch.cxx:31
void throwSalomeEx(const char *txt)
Definition: SMESH_TryCatch.cxx:26
void printErrorInDebugMode(const char *txt)
Definition: SMESH_TryCatch.cxx:42
const char * returnError(const char *txt)
Definition: SMESH_TryCatch.cxx:37