Salome HOME
Copyright update: 2016
[modules/smesh.git] / src / SMESHUtils / SMESH_TryCatch.hxx
1 // Copyright (C) 2007-2016  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 // 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 <Basics_OCCTVersion.hxx>
37 #include <utilities.h>
38
39 // IMPORTANT: include this file _after_ OCC ones, else OCC_CATCH_SIGNALS can be undefined!
40
41 #ifndef OCC_CATCH_SIGNALS
42 #define OCC_CATCH_SIGNALS
43 #endif
44
45 // Define macros to catch and convert some of possible exceptions into text or SALOME_Exception
46
47 //-------------------------------------------------------------------------------------
48 #define SMESH_TRY                               \
49   try {                                         \
50   OCC_CATCH_SIGNALS                             \
51
52 //-------------------------------------------------------------------------------------
53 // A macro to add a custom catch clause to SMESH_CATCH
54 // To add your own catch close, define SMY_OWN_CATCH macro before including this file.
55 #ifndef SMY_OWN_CATCH
56 #define SMY_OWN_CATCH
57 #endif
58
59 //-------------------------------------------------------------------------------------
60 // A macro allowing to retrieve a result returned by onExceptionFun
61 #define SMESH_CAUGHT
62
63 //-------------------------------------------------------------------------------------
64 // A macro makes description of a caught exception and calls onExceptionFun(const char*).
65 // Two onExceptionFun() are defined here: SMESH::throwSalomeEx() and SMESH::doNothing().
66 // To add your own catch close, define SMY_OWN_CATCH macro before including this file.
67
68 #define SMESH_CATCH( onExceptionFun )                                   \
69   }                                                                     \
70   catch (Standard_Failure& ex)                                          \
71   {                                                                     \
72     SMESH_Comment text("OCCT Exception: ");                             \
73     text << ": " << ex.DynamicType()->Name();                           \
74     if ( ex.GetMessageString() && strlen( ex.GetMessageString() ))      \
75       text << ": " << ex.GetMessageString();                            \
76     SMESH_CAUGHT onExceptionFun( text );                                \
77   }                                                                     \
78   catch ( ::SMESH_ComputeError& ce )                                    \
79   {                                                                     \
80     if ( !ce.myComment.empty() )                                        \
81       SMESH_CAUGHT onExceptionFun( ce.myComment.c_str() );              \
82     else if ( ce.IsCommon() )                                           \
83       SMESH_CAUGHT onExceptionFun( ce.CommonName().c_str() );           \
84     else                                                                \
85       SMESH_CAUGHT onExceptionFun                                       \
86         (SMESH_Comment("SMESH_ComputeError: ") << ce.myName );          \
87   }                                                                     \
88   catch ( const std::exception& ex)                                     \
89   {                                                                     \
90     SMESH_CAUGHT onExceptionFun( ex.what() );                           \
91   }                                                                     \
92                                                                         \
93   SMY_OWN_CATCH                                                         \
94                                                                         \
95   catch (...)                                                           \
96   {                                                                     \
97     SMESH_CAUGHT onExceptionFun("Unknown Exception caught");            \
98   }
99
100 //-------------------------------------------------------------------------------------
101 // Functions that can be used as an argument of SMESH_CATCH
102
103 namespace SMESH
104 {
105   SMESHUtils_EXPORT void throwSalomeEx(const char* txt);
106   SMESHUtils_EXPORT void doNothing(const char* txt);
107 }
108
109 #endif