C++ Logo

std-proposals

Advanced search

Re: Stacktrace from exception

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Thu, 29 Apr 2021 14:22:01 +0300
On 4/29/21 1:13 PM, Edward Catmur wrote:
> On Thu, 29 Apr 2021 at 10:56, Andrey Semashev via Std-Proposals
> <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]>>
> wrote:
>
> > The compiler can determine whether those could be called by the
> handler,
> > and suppress stacktrace if they are not.
>
> In general, no it can't. Static code analysis doesn't work when you
> need
> a definitive answer because the compiler may not see the whole program.
>
>
> If you're using exceptions for control flow the compiler will be able to
> see the whole of the program accessible from the catch block.

I don't see how.

> The only case when you'd use std::nested_exception is when you catch
> one
> exception (say, from library A), and you want to transform it to your
> own domain (say, that of your library B), and you want to retain the
> original for better diagnostics or special handling in the caller. Then
> you would wrap the original exception in std::nested_exception and
> throw
> it as your exception of domain B. Note that the caller would be
> catching
> exception of domain B, he will not be able to catch an exception of
> domain A. And that is exactly what is intended in this case.
>
>
> Yes, that's the normal case.

That's far from normal case in my experience. In fact, I'd be hard
pressed to remember a single time when I used std::nested_exception
while I'm attaching data to exceptions all the time - literally, on
every throw, if you count source location.

> https://www.boost.org/doc/libs/1_76_0/libs/exception/doc/get_error_info.html
> <https://www.boost.org/doc/libs/1_76_0/libs/exception/doc/get_error_info.html>
> >
> <https://www.boost.org/doc/libs/1_76_0/libs/exception/doc/get_error_info.html <https://www.boost.org/doc/libs/1_76_0/libs/exception/doc/get_error_info.html>>
> >
> > The ability to format all attached data along with the
> exception itself
> >
> (https://www.boost.org/doc/libs/1_76_0/libs/exception/doc/diagnostic_information.html <https://www.boost.org/doc/libs/1_76_0/libs/exception/doc/diagnostic_information.html>
> >
> <https://www.boost.org/doc/libs/1_76_0/libs/exception/doc/diagnostic_information.html <https://www.boost.org/doc/libs/1_76_0/libs/exception/doc/diagnostic_information.html>>)
> >
> > is just a cherry on top.
> >
> > That only works if you know the type of the item. There's no way to
> > iterate over items of unknown type individually.
>
> Ok, but do you need to iterate? In order to process the attached data,
> you do need to know its type anyway, so I see no problem here.
>
> You have the type and the value converted to string (type-erased on
> construction). That's all you need for diagnostics.

Again, you have the attached value of its original type. And yes, you
can use it for handling the exception, including for business logic; I
certainly do this in my code. That is besides formatting all the
attached data to a string for diagnostics.

Received on 2021-04-29 06:22:07