[xsd-users] Copying xml_schema::exception

Boris Kolpackov boris at codesynthesis.com
Fri Aug 1 10:27:12 EDT 2008


Hi Manav,

Manav Rathi <manav.rathi at incainformatics.com> writes:

> I need to store a copy of the xml_schema::exception that I catch at a
> certain point in my code (I cannot propogate the exception at the point
> where I am catching it, so I thought I might store it and then throw it
> later from an appropriate function).
> 
> Using the xml_schema::exception copy_constructor will lead to slicing. Any
> other ideas on how I might polymorphically copy it ?

There is no way to clone the exception polymorphically. Even if there 
was a way, you won't be able to re-throw it without slicing.

If all you need when you are handling the exception is to print it then
you can do that when you first catch it and store the string instead of
the exception:

try
{
  ...
}
catch (const xml_schema::exception& e)
{
  std::ostringstream os;
  os << e;
  std::string s = os.str ();
  // store s
}

Otherwise, the only way would be catch and handle each exception 
individually. Perhaps a little template like this will be helpful:

struct holder
{
  virtual ~holder () {}
  virtual void rethrow ();
};

template <typename X>
struct holder_impl
{
  holder_impl (const X& x)
    : x_ (x)

  virtual void rethrow ()
  {
    throw x_;
  }

private:
  X x_;
};


std::auto_ptr<holder> h;

try
{
  ...
}
catch (const xml_schema::parsing& e)
{
  h.reset (new holder_impl<xml_schema::parsing> (x));
}
catch (const xml_schema::expected_element& e)
{
  h.reset (new holder_impl<xml_schema::expected_element> (x));
}

...

if (h.get () != 0)
  h->rethrow ();

Boris




More information about the xsd-users mailing list