fix doc example typo
[boost.git] / boost / mpi / exception.hpp
blob12523077cc9ca280595dbaeb22b1dc2b575909f1
1 // Copyright (C) 2005-2006 Douglas Gregor <doug.gregor -at- gmail.com>.
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 /** @file exception.hpp
9 * This header provides exception classes that report MPI errors to
10 * the user and macros that translate MPI error codes into Boost.MPI
11 * exceptions.
13 #ifndef BOOST_MPI_EXCEPTION_HPP
14 #define BOOST_MPI_EXCEPTION_HPP
16 #include <boost/mpi/config.hpp>
17 #include <exception>
18 #include <string>
19 #include <boost/config.hpp>
20 #include <boost/throw_exception.hpp>
22 namespace boost { namespace mpi {
24 /** @brief Catch-all exception class for MPI errors.
26 * Instances of this class will be thrown when an MPI error
27 * occurs. MPI failures that trigger these exceptions may or may not
28 * be recoverable, depending on the underlying MPI
29 * implementation. Consult the documentation for your MPI
30 * implementation to determine the effect of MPI errors.
32 class BOOST_MPI_DECL exception : public std::exception
34 public:
35 /**
36 * Build a new @c exception exception.
38 * @param routine The MPI routine in which the error
39 * occurred. This should be a pointer to a string constant: it
40 * will not be copied.
42 * @param result_code The result code returned from the MPI
43 * routine that aborted with an error.
45 exception(const char* routine, int result_code);
47 virtual ~exception() throw();
49 /**
50 * A description of the error that occurred.
52 virtual const char * what () const throw ()
54 return this->message.c_str();
57 /** Retrieve the name of the MPI routine that reported the error. */
58 const char* routine() const { return routine_; }
60 /**
61 * @brief Retrieve the result code returned from the MPI routine
62 * that reported the error.
64 int result_code() const { return result_code_; }
66 /**
67 * @brief Returns the MPI error class associated with the error that
68 * triggered this exception.
70 int error_class() const
72 int result;
73 MPI_Error_class(result_code_, &result);
74 return result;
77 protected:
78 /// The MPI routine that triggered the error
79 const char* routine_;
81 /// The failed result code reported by the MPI implementation.
82 int result_code_;
84 /// The formatted error message
85 std::string message;
88 /**
89 * Call the MPI routine MPIFunc with arguments Args (surrounded by
90 * parentheses). If the result is not MPI_SUCCESS, use
91 * boost::throw_exception to throw an exception or abort, depending on
92 * BOOST_NO_EXCEPTIONS.
94 #define BOOST_MPI_CHECK_RESULT( MPIFunc, Args ) \
95 { \
96 int _check_result = MPIFunc Args; \
97 if (_check_result != MPI_SUCCESS) \
98 boost::throw_exception(boost::mpi::exception(#MPIFunc, \
99 _check_result)); \
102 } } // end namespace boost::mpi
104 #endif // BOOST_MPI_EXCEPTION_HPP