Salome HOME
updated copyright message
[modules/kernel.git] / src / DSC / DSC_User / test_DSC_Exception.cxx
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
23 //  File   : test_DSC_Exception.cxx
24 //  Author : Eric Fayolle (EDF)
25 //  Module : KERNEL
26 //
27 #include "DSC_Exception.hxx"
28
29 //Compiler ce fichier en mode _DEBUG_ et sans _DEBUG_
30 class A {
31
32 public:
33   DSC_EXCEPTION(Exp1)
34   DSC_EXCEPTION(Exp2)
35
36   A() {};
37   virtual ~A() {};
38   // La salome exception ne permet pas de passer une chaine ""
39   void lanceException1_1() { throw Exp1("_");}
40   void lanceException1_2() { throw Exp1("Ceci est l'exception 1_2");}
41   void lanceException1_3() { throw Exp1(LOC("Ceci est l'exception 1_3"));}
42   void lanceException1_4() { throw Exp1(LOC(OSS() << "Ceci est l'exeption 1_4" )); }
43   void lanceException1_5() { 
44     int a=1;
45     throw Exp1(LOC(OSS() << "Ceci est l'exeption 1_5 avec la valeur A : " << a )); }
46   void lanceException1_6() {
47     Exp1 exp1(LOC(OSS() << "Ceci est l'exeption 1_6"));
48     std::cout << "Affichage de exp1.what() dans lanceException1_6() " << exp1.what() << std::endl;
49     throw Exp1(exp1); 
50   }
51   void lanceException1_7() {
52     throw Exp1(LOC(OSS() << "Ceci est l'exeption 1_7"));
53    }
54 };
55
56 DSC_EXCEPTION_CXX(A,Exp1)
57 DSC_EXCEPTION_CXX(A,Exp2)
58
59
60 int main() {
61
62   A a;
63   
64
65   try {
66     a.lanceException1_1();
67   }
68   catch (  const A::Exp1  & ex ) {
69     std::cout << "Exception 1 bien reçue" << std::endl;
70     std::cout << ex.what() << std::endl;
71   } catch ( const DSC_Exception & dscE ) {
72     std::cout << "Exception DSC  reçue mais aurait dû recevoir Exception 1" << std::endl;
73   } catch ( ...) {
74     std::cout << "Exception ... reçue mais aurait dû recevoir Exception 1" << std::endl;
75   }
76
77   try {
78     a.lanceException1_2();
79   }
80   // Essai par valeur (ne pas faire çà !)
81   catch (  A::Exp1&  ex ) {
82     std::cout << "Exception 1 bien reçue" << std::endl;
83     std::cout << ex.what() << std::endl;
84   } catch ( const DSC_Exception & dscE ) {
85     std::cout << "Exception DSC  reçue mais aurait dû recevoir Exception 1" << std::endl;
86   } catch ( ...) {
87     std::cout << "Exception ... reçue mais aurait dû recevoir Exception 1" << std::endl;
88   }
89
90   try {
91     a.lanceException1_3();
92   }
93   catch ( const A::Exp1 & ex ) {
94     std::cout << "Exception 1 bien reçue" << std::endl;
95     std::cout << ex.what() << std::endl;
96   } catch ( const DSC_Exception & dscE ) {
97     std::cout << "Exception DSC  reçue mais aurait dû recevoir Exception 1" << std::endl;
98   } catch ( ...) {
99     std::cout << "Exception ... reçue mais aurait dû recevoir Exception 1" << std::endl;
100   }
101
102   try {
103     a.lanceException1_4();
104   }
105   catch ( const A::Exp1 & ex ) {
106     std::cout << "Exception 1 bien reçue" << std::endl;
107     std::cout << ex.what() << std::endl;
108   } catch ( const DSC_Exception & dscE ) {
109     std::cout << "Exception DSC  reçue mais aurait dû recevoir Exception 1" << std::endl;
110   } catch ( ...) {
111     std::cout << "Exception ... reçue mais aurait dû recevoir Exception 1" << std::endl;
112   }
113
114   try {
115     a.lanceException1_5();
116   }
117   catch ( const A::Exp1 & ex ) {
118     std::cout << "Exception 1 bien reçue" << std::endl;
119     std::cout << ex.what() << std::endl;
120   } catch ( const DSC_Exception & dscE ) {
121     std::cout << "Exception DSC  reçue mais aurait dû recevoir Exception 1" << std::endl;
122   } catch ( ...) {
123     std::cout << "Exception ... reçue mais aurait dû recevoir Exception 1" << std::endl;
124   }
125
126   try {
127     a.lanceException1_6();
128   }
129   catch ( SALOME_Exception& ex ) {
130     std::cout << "Exception SALOME bien reçue" << std::endl;
131     std::cout << ex.what() << std::endl;
132   } catch ( ...) {
133     std::cout << "Exception ... reçue mais aurait dû recevoir Exception SALOME" << std::endl;
134   }
135
136   try {
137     a.lanceException1_6();
138   }
139   catch ( const SALOME_Exception & ex ) {
140     std::cout << "Exception SALOME bien reçue" << std::endl;
141     std::cout << ex.what() << std::endl;
142   } catch ( ...) {
143     std::cout << "Exception ... reçue mais aurait dû recevoir Exception SALOME" << std::endl;
144   }
145
146   try {
147     a.lanceException1_7();
148   } catch ( const DSC_Exception & ex ) {
149     std::cout << "Exception DSC bien reçue" << std::endl;
150     std::cout << ex.what() << std::endl;
151   } catch ( const SALOME_Exception & ex ) {
152     std::cout << "Exception SALOME reçue mais aurais dû recevoir une exception DSC" << std::endl;
153     std::cout << ex.what() << std::endl;
154   } catch ( ...) {
155     std::cout << "Exception ... reçue mais aurait dû recevoir Exception DSC" << std::endl;
156   }
157
158 }