1 | // (C) Copyright Gennadiy Rozental 2003-2005. |
---|
2 | // Distributed under the Boost Software License, Version 1.0. |
---|
3 | // (See accompanying file LICENSE_1_0.txt or copy at |
---|
4 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
5 | |
---|
6 | // See http://www.boost.org/libs/test for the library home page. |
---|
7 | |
---|
8 | #include <boost/test/execution_monitor.hpp> |
---|
9 | #include <boost/test/utils/basic_cstring/io.hpp> |
---|
10 | |
---|
11 | #include <iostream> |
---|
12 | |
---|
13 | struct my_exception1 |
---|
14 | { |
---|
15 | explicit my_exception1( int res_code ) : m_res_code( res_code ) {} |
---|
16 | |
---|
17 | int m_res_code; |
---|
18 | }; |
---|
19 | |
---|
20 | struct my_exception2 |
---|
21 | { |
---|
22 | explicit my_exception2( int res_code ) : m_res_code( res_code ) {} |
---|
23 | |
---|
24 | int m_res_code; |
---|
25 | }; |
---|
26 | |
---|
27 | namespace { |
---|
28 | |
---|
29 | class dangerous_call { |
---|
30 | public: |
---|
31 | dangerous_call( int argc ) : m_argc( argc ) {} |
---|
32 | |
---|
33 | int operator()() |
---|
34 | { |
---|
35 | // here we perform some operation under monitoring that could throw my_exception |
---|
36 | if( m_argc < 2 ) |
---|
37 | throw my_exception1( 23 ); |
---|
38 | if( m_argc > 3 ) |
---|
39 | throw my_exception2( 45 ); |
---|
40 | else if( m_argc > 2 ) |
---|
41 | throw "too many args"; |
---|
42 | |
---|
43 | return 1; |
---|
44 | } |
---|
45 | |
---|
46 | private: |
---|
47 | // Data members |
---|
48 | int m_argc; |
---|
49 | }; |
---|
50 | |
---|
51 | void translate_my_exception1( my_exception1 const& ex ) |
---|
52 | { |
---|
53 | std::cout << "Caught my_exception1(" << ex.m_res_code << ")"<< std::endl; |
---|
54 | } |
---|
55 | |
---|
56 | void translate_my_exception2( my_exception2 const& ex ) |
---|
57 | { |
---|
58 | std::cout << "Caught my_exception2(" << ex.m_res_code << ")"<< std::endl; |
---|
59 | } |
---|
60 | |
---|
61 | } // local_namespace |
---|
62 | |
---|
63 | int |
---|
64 | main( int argc , char *[] ) |
---|
65 | { |
---|
66 | ::boost::execution_monitor ex_mon; |
---|
67 | |
---|
68 | ex_mon.register_exception_translator<my_exception1>( &translate_my_exception1 ); |
---|
69 | ex_mon.register_exception_translator<my_exception2>( &translate_my_exception2 ); |
---|
70 | |
---|
71 | try { |
---|
72 | ex_mon.execute( ::boost::unit_test::callback0<int>( dangerous_call( argc ) ) ); |
---|
73 | } |
---|
74 | catch ( boost::execution_exception const& ex ) { |
---|
75 | std::cout << "Caught exception: " << ex.what() << std::endl; |
---|
76 | } |
---|
77 | |
---|
78 | return 0; |
---|
79 | } |
---|
80 | |
---|
81 | // EOF |
---|