Files
exception/example/error_info_2.cpp

46 lines
1.1 KiB
C++
Raw Normal View History

2009-04-07 03:02:15 +00:00
//Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
2008-03-04 01:41:17 +00:00
//Distributed under the Boost Software License, Version 1.0. (See accompanying
//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//This example shows how to add arbitrary data to active exception objects.
#include <boost/exception/all.hpp>
2008-03-04 01:41:17 +00:00
#include <boost/shared_ptr.hpp>
#include <stdio.h>
#include <errno.h>
//
struct file_read_error: virtual boost::exception { };
2008-03-04 01:41:17 +00:00
void
file_read( FILE * f, void * buffer, size_t size )
{
2008-03-04 01:41:17 +00:00
if( size!=fread(buffer,1,size,f) )
throw file_read_error() << boost::errinfo_errno(errno);
}
2008-03-04 01:41:17 +00:00
//
boost::shared_ptr<FILE> file_open( char const * file_name, char const * mode );
void file_read( FILE * f, void * buffer, size_t size );
void
parse_file( char const * file_name )
{
2008-03-04 01:41:17 +00:00
boost::shared_ptr<FILE> f = file_open(file_name,"rb");
assert(f);
try
{
char buf[1024];
2008-03-04 01:41:17 +00:00
file_read( f.get(), buf, sizeof(buf) );
}
2008-03-04 01:41:17 +00:00
catch(
boost::exception & e )
{
e << boost::errinfo_file_name(file_name);
2008-03-04 01:41:17 +00:00
throw;
}
}