sl@0
|
1 |
// boost/catch_exceptions.hpp -----------------------------------------------//
|
sl@0
|
2 |
|
sl@0
|
3 |
// Copyright Beman Dawes 1995-2001. Distributed under the Boost
|
sl@0
|
4 |
// Software License, Version 1.0. (See accompanying file
|
sl@0
|
5 |
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
sl@0
|
6 |
|
sl@0
|
7 |
// See http://www.boost.org/libs/test for documentation.
|
sl@0
|
8 |
|
sl@0
|
9 |
// Revision History
|
sl@0
|
10 |
// 13 Jun 01 report_exception() made inline. (John Maddock, Jesse Jones)
|
sl@0
|
11 |
// 26 Feb 01 Numerous changes suggested during formal review. (Beman)
|
sl@0
|
12 |
// 25 Jan 01 catch_exceptions.hpp code factored out of cpp_main.cpp.
|
sl@0
|
13 |
// 22 Jan 01 Remove test_tools dependencies to reduce coupling.
|
sl@0
|
14 |
// 5 Nov 00 Initial boost version (Beman Dawes)
|
sl@0
|
15 |
|
sl@0
|
16 |
#ifndef BOOST_CATCH_EXCEPTIONS_HPP
|
sl@0
|
17 |
#define BOOST_CATCH_EXCEPTIONS_HPP
|
sl@0
|
18 |
|
sl@0
|
19 |
// header dependencies are deliberately restricted to the standard library
|
sl@0
|
20 |
// to reduce coupling to other boost libraries.
|
sl@0
|
21 |
#include <string> // for string
|
sl@0
|
22 |
#include <new> // for bad_alloc
|
sl@0
|
23 |
#include <typeinfo> // for bad_cast, bad_typeid
|
sl@0
|
24 |
#include <exception> // for exception, bad_exception
|
sl@0
|
25 |
#include <stdexcept> // for std exception hierarchy
|
sl@0
|
26 |
#include <boost/cstdlib.hpp> // for exit codes
|
sl@0
|
27 |
# if __GNUC__ != 2 || __GNUC_MINOR__ > 96
|
sl@0
|
28 |
# include <ostream> // for ostream
|
sl@0
|
29 |
# else
|
sl@0
|
30 |
# include <iostream> // workaround GNU missing ostream header
|
sl@0
|
31 |
# endif
|
sl@0
|
32 |
|
sl@0
|
33 |
# if defined(__BORLANDC__) && (__BORLANDC__ <= 0x0551)
|
sl@0
|
34 |
# define BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT
|
sl@0
|
35 |
# endif
|
sl@0
|
36 |
|
sl@0
|
37 |
#if defined(MPW_CPLUS) && (MPW_CPLUS <= 0x890)
|
sl@0
|
38 |
# define BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT
|
sl@0
|
39 |
namespace std { class bad_typeid { }; }
|
sl@0
|
40 |
# endif
|
sl@0
|
41 |
|
sl@0
|
42 |
namespace boost
|
sl@0
|
43 |
{
|
sl@0
|
44 |
|
sl@0
|
45 |
namespace detail
|
sl@0
|
46 |
{
|
sl@0
|
47 |
// A separate reporting function was requested during formal review.
|
sl@0
|
48 |
inline void report_exception( std::ostream & os,
|
sl@0
|
49 |
const char * name, const char * info )
|
sl@0
|
50 |
{ os << "\n** uncaught exception: " << name << " " << info << std::endl; }
|
sl@0
|
51 |
}
|
sl@0
|
52 |
|
sl@0
|
53 |
// catch_exceptions ------------------------------------------------------//
|
sl@0
|
54 |
|
sl@0
|
55 |
template< class Generator > // Generator is function object returning int
|
sl@0
|
56 |
int catch_exceptions( Generator function_object,
|
sl@0
|
57 |
std::ostream & out, std::ostream & err )
|
sl@0
|
58 |
{
|
sl@0
|
59 |
int result = 0; // quiet compiler warnings
|
sl@0
|
60 |
bool exception_thrown = true; // avoid setting result for each excptn type
|
sl@0
|
61 |
|
sl@0
|
62 |
#ifndef BOOST_NO_EXCEPTIONS
|
sl@0
|
63 |
try
|
sl@0
|
64 |
{
|
sl@0
|
65 |
#endif
|
sl@0
|
66 |
result = function_object();
|
sl@0
|
67 |
exception_thrown = false;
|
sl@0
|
68 |
#ifndef BOOST_NO_EXCEPTIONS
|
sl@0
|
69 |
}
|
sl@0
|
70 |
|
sl@0
|
71 |
// As a result of hard experience with strangely interleaved output
|
sl@0
|
72 |
// under some compilers, there is a lot of use of endl in the code below
|
sl@0
|
73 |
// where a simple '\n' might appear to do.
|
sl@0
|
74 |
|
sl@0
|
75 |
// The rules for catch & arguments are a bit different from function
|
sl@0
|
76 |
// arguments (ISO 15.3 paragraphs 18 & 19). Apparently const isn't
|
sl@0
|
77 |
// required, but it doesn't hurt and some programmers ask for it.
|
sl@0
|
78 |
|
sl@0
|
79 |
catch ( const char * ex )
|
sl@0
|
80 |
{ detail::report_exception( out, "", ex ); }
|
sl@0
|
81 |
catch ( const std::string & ex )
|
sl@0
|
82 |
{ detail::report_exception( out, "", ex.c_str() ); }
|
sl@0
|
83 |
|
sl@0
|
84 |
// std:: exceptions
|
sl@0
|
85 |
catch ( const std::bad_alloc & ex )
|
sl@0
|
86 |
{ detail::report_exception( out, "std::bad_alloc:", ex.what() ); }
|
sl@0
|
87 |
|
sl@0
|
88 |
# ifndef BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT
|
sl@0
|
89 |
catch ( const std::bad_cast & ex )
|
sl@0
|
90 |
{ detail::report_exception( out, "std::bad_cast:", ex.what() ); }
|
sl@0
|
91 |
catch ( const std::bad_typeid & ex )
|
sl@0
|
92 |
{ detail::report_exception( out, "std::bad_typeid:", ex.what() ); }
|
sl@0
|
93 |
# else
|
sl@0
|
94 |
catch ( const std::bad_cast & )
|
sl@0
|
95 |
{ detail::report_exception( out, "std::bad_cast", "" ); }
|
sl@0
|
96 |
catch ( const std::bad_typeid & )
|
sl@0
|
97 |
{ detail::report_exception( out, "std::bad_typeid", "" ); }
|
sl@0
|
98 |
# endif
|
sl@0
|
99 |
|
sl@0
|
100 |
catch ( const std::bad_exception & ex )
|
sl@0
|
101 |
{ detail::report_exception( out, "std::bad_exception:", ex.what() ); }
|
sl@0
|
102 |
catch ( const std::domain_error & ex )
|
sl@0
|
103 |
{ detail::report_exception( out, "std::domain_error:", ex.what() ); }
|
sl@0
|
104 |
catch ( const std::invalid_argument & ex )
|
sl@0
|
105 |
{ detail::report_exception( out, "std::invalid_argument:", ex.what() ); }
|
sl@0
|
106 |
catch ( const std::length_error & ex )
|
sl@0
|
107 |
{ detail::report_exception( out, "std::length_error:", ex.what() ); }
|
sl@0
|
108 |
catch ( const std::out_of_range & ex )
|
sl@0
|
109 |
{ detail::report_exception( out, "std::out_of_range:", ex.what() ); }
|
sl@0
|
110 |
catch ( const std::range_error & ex )
|
sl@0
|
111 |
{ detail::report_exception( out, "std::range_error:", ex.what() ); }
|
sl@0
|
112 |
catch ( const std::overflow_error & ex )
|
sl@0
|
113 |
{ detail::report_exception( out, "std::overflow_error:", ex.what() ); }
|
sl@0
|
114 |
catch ( const std::underflow_error & ex )
|
sl@0
|
115 |
{ detail::report_exception( out, "std::underflow_error:", ex.what() ); }
|
sl@0
|
116 |
catch ( const std::logic_error & ex )
|
sl@0
|
117 |
{ detail::report_exception( out, "std::logic_error:", ex.what() ); }
|
sl@0
|
118 |
catch ( const std::runtime_error & ex )
|
sl@0
|
119 |
{ detail::report_exception( out, "std::runtime_error:", ex.what() ); }
|
sl@0
|
120 |
catch ( const std::exception & ex )
|
sl@0
|
121 |
{ detail::report_exception( out, "std::exception:", ex.what() ); }
|
sl@0
|
122 |
|
sl@0
|
123 |
catch ( ... )
|
sl@0
|
124 |
{ detail::report_exception( out, "unknown exception", "" ); }
|
sl@0
|
125 |
#endif // BOOST_NO_EXCEPTIONS
|
sl@0
|
126 |
|
sl@0
|
127 |
if ( exception_thrown ) result = boost::exit_exception_failure;
|
sl@0
|
128 |
|
sl@0
|
129 |
if ( result != 0 && result != exit_success )
|
sl@0
|
130 |
{
|
sl@0
|
131 |
out << std::endl << "**** returning with error code "
|
sl@0
|
132 |
<< result << std::endl;
|
sl@0
|
133 |
err
|
sl@0
|
134 |
<< "********** errors detected; see stdout for details ***********"
|
sl@0
|
135 |
<< std::endl;
|
sl@0
|
136 |
}
|
sl@0
|
137 |
#if !defined(BOOST_NO_CPP_MAIN_SUCCESS_MESSAGE)
|
sl@0
|
138 |
else { out << std::flush << "no errors detected" << std::endl; }
|
sl@0
|
139 |
#endif
|
sl@0
|
140 |
return result;
|
sl@0
|
141 |
} // catch_exceptions
|
sl@0
|
142 |
|
sl@0
|
143 |
} // boost
|
sl@0
|
144 |
|
sl@0
|
145 |
#endif // BOOST_CATCH_EXCEPTIONS_HPP
|
sl@0
|
146 |
|