← Back to team overview

dolfin team mailing list archive

Re: GenericTensor::down_cast() should throw std::bad_cast instead of invoking dolfin::error

 

On Tue, 2010-08-03 at 07:51 +0200, Florian Rathgeber wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> On 02.08.2010 18:50, Garth N. Wells wrote:
> > On Mon, 2010-08-02 at 10:33 +0200, Anders Logg wrote:
> >> On Sun, Aug 01, 2010 at 06:35:22PM +0100, Garth N. Wells wrote:
> >>> On Sun, 2010-08-01 at 12:40 +0200, Florian Rathgeber wrote:
> >>>> -----BEGIN PGP SIGNED MESSAGE-----
> >>>> Hash: SHA1
> >>>>
> >>>> Hi,
> >>>>
> >>>> When using GenericTensor::down_cast() for a tentative down cast it would
> >>>> be helpful if it would throw std::bad_alloc (as any dynamic_cast would
> >>>> do)
> >>>
> >>> Do you mean std::bad_cast?
> >>>
> >>>> instead of invoking dolfin::error. Currently you would have to catch
> >>>> std::runtime_error and check what() to distinguish a failed cast from
> >>>> another dolfin::error, which is pretty inconvenient. Is there a specific
> >>>> reason why it is implemented this way?
> >>>>
> >>>
> >>> Not really.
> >>>
> >>> It's not too clear to me what you'd like. Would you prefer that
> >>> down_cast doesn't throw an error, but leave it up to the programmer to
> >>> check that a cast was successful, or just that DOLFIN catch a
> >>> std::bad_cast and print more information before throwing an error?
> >>
> >> I think the best would be if DOLFIN caught the error, then wrote an
> >> informative message (since it knows exactly what went wrong) and then
> >> threw bad_cast.
> >>
> > 
> > Looks like there isn't much that can be done - std::bad_cast is only
> > thrown when casting references, not pointers (as we do internally). From
> > what I've read, our way of checking for a null pointer is correct.
> 
> If down_cast() were implemented as casting the reference directly,
> dynamic_cast would throw std::bad_cast if it fails.
> 
> The check for the null pointer is correct, my point is that in this case
> not a std::runtime_error should be thrown (which happens by calling
> dolfin::error), but instead the message should be printed e.g. by
> dolfin::warning and then a std::bad_cast thrown. That was Anders'
> suggestion if I got that right.
> 
> In that way down_cast() could be used as a tentative cast which is not
> really possible if it throws the same exception that could be caused by
> and DOLFIN error.
> 

GenericTensor::down_cast now catches an exception and then throws an
error. It was simpler than I thought because return statements can be
used inside a try block (which I didn't know).

Garth

> Florian
> 
> > Garth
> > 
> >> --
> >> Anders
> > 
> > 
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v2.0.12 (MingW32)
> 
> iEYEARECAAYFAkxXrlAACgkQ8Z6llsctAxaMqgCfSqDc/Ii5B41knpwHFVWsjSLF
> H3AAn1pOlxXJd/PnDbVWNcyF5GNNmPt8
> =uibh
> -----END PGP SIGNATURE-----
> 





Follow ups

References