error_info_1.cpp 875 B

1234567891011121314151617181920212223242526272829303132333435
  1. //Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
  2. //Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. //This example shows how to add data to boost::exception objects at the
  5. //point of the throw, and how to retrieve that data at the point of the catch.
  6. #include <boost/exception/all.hpp>
  7. #include <iostream>
  8. typedef boost::error_info<struct tag_my_info,int> my_info; //(1)
  9. struct my_error: virtual boost::exception, virtual std::exception { }; //(2)
  10. void
  11. f()
  12. {
  13. throw my_error() << my_info(42); //(3)
  14. }
  15. void
  16. g()
  17. {
  18. try
  19. {
  20. f();
  21. }
  22. catch(
  23. my_error & x )
  24. {
  25. if( int const * mi=boost::get_error_info<my_info>(x) )
  26. std::cerr << "My info: " << *mi;
  27. }
  28. }