PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/phoenix/test/statement/exceptions.cpp

http://hadesmem.googlecode.com/
C++ | 106 lines | 81 code | 18 blank | 7 comment | 2 complexity | 44f314a06784c575a8f8205db2b7d28f MD5 | raw file
  1/*=============================================================================
  2    Copyright (c) 2005-2007 Dan Marsden
  3    Copyright (c) 2005-2007 Joel de Guzman
  4
  5    Distributed under the Boost Software License, Version 1.0. (See accompanying 
  6    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7==============================================================================*/
  8
  9#include <stdexcept>
 10#include <string>
 11
 12#include <boost/phoenix/core.hpp>
 13#include <boost/phoenix/operator.hpp>
 14#include <boost/phoenix/statement.hpp>
 15
 16#include <boost/detail/lightweight_test.hpp>
 17
 18int main()
 19{
 20    using boost::phoenix::throw_;
 21    using boost::phoenix::try_;
 22    using boost::phoenix::ref;
 23    using std::exception;
 24    using std::string;
 25    using std::runtime_error;
 26
 27    {
 28        try
 29        {
 30            throw_(runtime_error("error"))();
 31            BOOST_ERROR("exception should have been thrown");
 32        }
 33        catch(runtime_error& err)
 34        {
 35            BOOST_TEST(err.what() == string("error"));
 36        }
 37    }
 38
 39    {
 40        try
 41        {
 42            try
 43            {
 44                throw runtime_error("error");
 45            }
 46            catch(exception&)
 47            {
 48                throw_()();
 49                BOOST_ERROR("exception should have been rethrown");
 50            }
 51        }
 52        catch(exception& err)
 53        {
 54            BOOST_TEST(err.what() == string("error"));
 55        }
 56    }
 57
 58    {
 59        bool caught_exception = false;
 60
 61        try_
 62        [ throw_(runtime_error("error")) ]
 63        .catch_<exception>()
 64        [ ref(caught_exception) = true ]();
 65
 66        BOOST_TEST(caught_exception);
 67    }
 68
 69    {
 70        bool caught_exception = false;
 71
 72        try_
 73        [ throw_(runtime_error("error")) ]
 74        .catch_all
 75        [ ref(caught_exception) = true ]();
 76        BOOST_TEST(caught_exception);
 77    }
 78
 79    {
 80        bool caught_correct_exception = false;
 81
 82        try_
 83            [ throw_(runtime_error("error")) ]
 84        .catch_<string>()
 85            [ ref(caught_correct_exception) = false ]
 86        .catch_<exception>()
 87            [ ref(caught_correct_exception) = true]();
 88
 89        BOOST_TEST(caught_correct_exception);
 90    }
 91
 92    {
 93        bool caught_correct_exception = false;
 94
 95        try_
 96            [ throw_(runtime_error("error")) ]
 97        .catch_<string>()
 98            [ ref(caught_correct_exception) = false ]
 99        .catch_all
100            [ ref(caught_correct_exception) = true]();
101
102        BOOST_TEST(caught_correct_exception);
103    }
104
105    return boost::report_errors();
106}