error_info_2.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 arbitrary data to active exception objects.
  5. #include <boost/exception/all.hpp>
  6. #include <boost/shared_ptr.hpp>
  7. #include <stdio.h>
  8. #include <errno.h>
  9. //
  10. struct file_read_error: virtual boost::exception { };
  11. void
  12. file_read( FILE * f, void * buffer, size_t size )
  13. {
  14. if( size!=fread(buffer,1,size,f) )
  15. throw file_read_error() << boost::errinfo_errno(errno);
  16. }
  17. //
  18. boost::shared_ptr<FILE> file_open( char const * file_name, char const * mode );
  19. void file_read( FILE * f, void * buffer, size_t size );
  20. void
  21. parse_file( char const * file_name )
  22. {
  23. boost::shared_ptr<FILE> f = file_open(file_name,"rb");
  24. assert(f);
  25. try
  26. {
  27. char buf[1024];
  28. file_read( f.get(), buf, sizeof(buf) );
  29. }
  30. catch(
  31. boost::exception & e )
  32. {
  33. e << boost::errinfo_file_name(file_name);
  34. throw;
  35. }
  36. }