Boost logo

Boost :

From: Dean Michael Berris (mikhailberis_at_[hidden])
Date: 2006-07-07 16:55:57

On 7/6/06, Emil Dotchevski <emildotchevski_at_[hidden]> wrote:
> template <class T>
> exception_info * get_exception_info( T & );
> It can be used in a catch( T & ) block, to check if the exception object has
> a sub-object of class exception_info.

Why return a pointer? How about considering the use of a reference
instead? Perhaps:

template < typename T >
exception_info & get_exception_info ( T & );

That way, the return can only be accessed using an exception_info
reference. Consider this example (which is considerably cleaner to
read, IMO) :

try {
  throw failed<read_error>() << wrap_string<tag_file_name>("example.txt");
} catch (read_error & e) {
  exception_info info = get_exception_info(e);
  if (info) {
    // deal with the info...

  // deal with the exception...

This also avoids the possibility of mis-use of pointers, because
client code can still do a cast of the pointer to refer to something
else -- which although it is hackish, is something you'd want to
avoid. It also makes the following possible:

std::cout << get_string<tag_file_name>(get_exception_info(e)) << std::endl;

Which is a little long for my taste, but readable nonetheless.


Dean Michael C. Berris
C/C++ Software Architect
Orange and Bronze Software Labs
Mobile: +639287291459
Email: dean [at] orangeandbronze [dot] com

Boost list run by bdawes at, gregod at, cpdaniel at, john at