Boost logo

Boost :

From: Hamish Mackenzie (boost_at_[hidden])
Date: 2002-02-18 11:31:20


On Mon, 2002-02-18 at 08:11, Herb Sutter wrote:
> Hi folks,
>
> Pardon if this has already been discussed on the list, but I'd like your
> feedback on this question: "What are the most needed or desired features in
> C++ for modern library writing in general and Boost in particular?"
>
> Two usual suspects:
> - typedef templates
> - typeof (with gcc semantics?)
>
> What else should be on the list? For example, is there language support that
> would assist type traits?

Catch Functions

A function placed inside catch() would catch exceptions based on the
argument type of the function then handle them only if the function
returned true.

If two or more functions have the same argument type the first one in
order to return true will handle the exception.

consider

// Throws exceptions with the error number stored as member
error_policy_1
{
public:
  class error : puiblic std::exception
  {
  public:
    error( int err ) : error_number( err ) {}
    int error_number;
  };

  // Called to handle errors
  void error( int err ) { throw error( err ); }

  template< int ErrorNumber >
  static bool check_error_number( const error &e )
    { return e.error_number == ErrorNumber }

  template< int ErrorNumber >
  typedef error type_to_catch;
}

// Throws exceptions with the error number as a template argument
error_policy_2
{
public:
  template< int ErrorNo >
  class error : puiblic std::exception {};

  // Called to handle errors
  void error( int err )
  {
    switch( err )
    {
    case 5 : throw error< 5 >();
    ....
    }
  }
  
  template< int ErrorNumber >
  static bool check_error_number( const error< ErrorNumber > &e )
    { return true; }

  template< int ErrorNumber >
  typedef error< ErrorNumber > type_to_catch;
}

template< typename ErrorPolicy >
class some_class
{
...
};

Now the problem is if you don't know the policy how do you catch the
exception?

Here is the code currently needed to catch both error 1 and 5 for both
policies

try
{
try
{
}
catch( ErrorPolicy::type_to_catch< 1 > & e )
{
  if( ErrorPolicy::check_error_number< 1 >( e ) )
  {
    // handle exception
  }
  else
  {
    throw;
  }
}
}
catch( ErrorPolicy::type_to_catch< 5 > & e )
{
  if( ErrorPolicy::check_error_number< 5 >( e ) )
  {
    // handle exception
  }
  else
  {
    throw;
  }
}

It would be nice to be able to just write
try
{
}
catch( Policy::check_error_number< 1 > )
{
}
catch( Policy::check_error_number< 5 > )
{
}

Hamish
 


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk