1 /* $Id: netest.cpp 574026 2018-11-05 17:41:17Z lavr $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author:
27 * Vsevolod Sandomirskiy
28 *
29 * File Description:
30 * Sample to test OS-independent C++ exceptions
31 */
32
33 #define __EXTENSIONS__
34 #define _POSIX_C_SOURCE 199506L
35
36 #include <ncbi_pch.hpp>
37
38 #ifdef UNIX
39 #include <signal.h>
40 #elif WIN32
41 #include <windows.h>
42 #endif
43
44 using namespace std;
45
46 #include <ncbiapp.hpp>
47 #include <ncbiexcp.hpp>
48
49 #include <vector>
50
51 class CMyApplication : public CNcbiApplication
52 {
53 public:
54
CMyApplication(int argc,char * argv[])55 CMyApplication( int argc, char* argv[] ) throw()
56 : CNcbiApplication( argc, argv ) {}
57
58 virtual void Exit( void ); // cleanup
59
60 virtual int Run();
61
fun()62 void fun() throw()
63 { throw "test"; }
64
65 };
66
Exit()67 void CMyApplication::Exit()
68 {
69 int x = 0;
70 x = 1 / x;
71 }
72
Run()73 int CMyApplication::Run()
74 {
75
76 try {
77
78 #ifdef UNIX
79 raise( SIGILL );
80 //abort();
81 #elif WIN32
82 RaiseException( STATUS_ACCESS_VIOLATION, 0, 0, 0 );
83 #endif
84
85 } catch( CNcbiOSException& e ) {
86 cout << e.what() << endl;
87 } catch( runtime_error e ) {
88 cout << e.what() << endl;
89 } catch( exception& e ) {
90 cout << e.what() << endl;
91 }
92
93 cout << "try 1 OK" << endl;
94
95 try {
96
97 #ifdef UNIX
98 raise( SIGFPE );
99 #elif WIN32
100 RaiseException( STATUS_BREAKPOINT, 0, 0, 0 );
101 #endif
102
103 } catch( CNcbiOSException& e ) {
104 cout << e.what() << endl;
105 } catch( runtime_error e ) {
106 cout << e.what() << endl;
107 } catch( exception& e ) {
108 cout << e.what() << endl;
109 }
110
111 cout << "try 2 OK" << endl;
112
113 try {
114
115 #ifdef UNIX
116 // this signal is blocked
117 raise( SIGHUP );
118 #elif WIN32
119 RaiseException( STATUS_INTEGER_OVERFLOW, 0, 0, 0 );
120 #endif
121 } catch( CNcbiOSException& e ) {
122 cout << e.what() << endl;
123 } catch( runtime_error e ) {
124 cout << e.what() << endl;
125 } catch( exception& e ) {
126 cout << e.what() << endl;
127 }
128
129 cout << "try 3 OK" << endl;
130
131 cout << "Done" << endl;
132
133 return 0;
134 }
135
136 template<class T>
137 class Test
138 {
139 public:
140
Get()141 static T* Get()
142 { cout << "Test generic\n"; return new T; }
143 };
144
145 template<>
146 class Test<int>
147 {
148 public:
149
Get()150 static int* Get()
151 { cout << "Test int\n"; return new int; }
152 };
153
GetF(T * t)154 template<class T> T* GetF(T* t)
155 { return new T; }
156
157 struct B
158 {};
159
160
main(int argc,char * argv[])161 int main( int argc, char* argv[] )
162 {
163
164 CMyApplication app( argc, argv );
165 int res = 1;
166
167 cout << "Start" << endl;
168
169 vector<B> v;
170
171 try {
172 app.Init();
173
174 cout << "before call\n";
175 int *ii = 0;
176 *ii = 7;
177 cout << "after call\n";
178
179 res = app.Run();
180
181 //CMyApplication* a( 0 , NULL );
182 //cout << "before fun\n";
183 //a.fun();
184 //cout << "after fun\n";
185
186 app.Exit();
187 } catch( CNcbiOSException& e ) {
188 cout << "Failed " << e.what() << endl;
189 return res;
190 } catch( bad_exception& ) {
191 cout << "Failed" << endl;
192 return res;
193 } catch( ... ) {
194 cout << "Failed" << endl;
195 return res;
196 }
197
198 return res;
199 }
200