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