1//Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
2
3//Distributed under the Boost Software License, Version 1.0. (See accompanying
4//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6//This example demonstrates the intended use of various commonly used
7//error_info typedefs provided by Boost Exception.
8
9#include <boost/exception/errinfo_api_function.hpp>
10#include <boost/exception/errinfo_at_line.hpp>
11#include <boost/exception/errinfo_errno.hpp>
12#include <boost/exception/errinfo_file_handle.hpp>
13#include <boost/exception/errinfo_file_name.hpp>
14#include <boost/exception/errinfo_file_open_mode.hpp>
15#include <boost/exception/info.hpp>
16#include <boost/throw_exception.hpp>
17#include <boost/shared_ptr.hpp>
18#include <boost/weak_ptr.hpp>
19#include <stdio.h>
20#include <errno.h>
21#include <exception>
22
23struct error : virtual std::exception, virtual boost::exception { };
24struct file_error : virtual error { };
25struct file_open_error: virtual file_error { };
26struct file_read_error: virtual file_error { };
27
28boost::shared_ptr<FILE>
29open_file( char const * file, char const * mode )
30 {
31 if( FILE * f=fopen(filename: file,modes: mode) )
32 return boost::shared_ptr<FILE>(f,fclose);
33 else
34 BOOST_THROW_EXCEPTION(
35 file_open_error() <<
36 boost::errinfo_api_function("fopen") <<
37 boost::errinfo_errno(errno) <<
38 boost::errinfo_file_name(file) <<
39 boost::errinfo_file_open_mode(mode) );
40 }
41
42size_t
43read_file( boost::shared_ptr<FILE> const & f, void * buf, size_t size )
44 {
45 size_t nr=fread(ptr: buf,size: 1,n: size,stream: f.get());
46 if( ferror(stream: f.get()) )
47 BOOST_THROW_EXCEPTION(
48 file_read_error() <<
49 boost::errinfo_api_function("fread") <<
50 boost::errinfo_errno(errno) <<
51 boost::errinfo_file_handle(f) );
52 return nr;
53 }
54

source code of boost/libs/exception/example/errinfos.cpp