Bug 78710 - suggest better exception text for stoi, stol, ...
Summary: suggest better exception text for stoi, stol, ...
Status: ASSIGNED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Jonathan Wakely
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-12-07 13:46 UTC by Nicolai Josuttis
Modified: 2023-07-13 17:10 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-03-08 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nicolai Josuttis 2016-12-07 13:46:36 UTC
stoi("hello") currently throws an exception where what() only outputs "stoi"
(nothing else).

The reason is (if I analyzed it correctly):
- stoi etc. pass their name (just "stoi") to __stoa as __name
- __stoa calls:
    std::__throw_invalid_argument(__name);
- which usually seems to call _GLIBCXX_THROW_OR_ABORT

Some component (either __stoa() or __throw_invalid_argument())
should extend the message at least a bit.
E.g. __stoa() might call
       std::__throw_invalid_argument("invalid argument for " + __name)
(using a valid syntax of course).
Comment 1 Nicolai Josuttis 2016-12-07 13:47:33 UTC
applies to any version AFAIK
Comment 2 Jonathan Wakely 2021-03-08 11:56:56 UTC
(In reply to Nicolai Josuttis from comment #0)
> stoi("hello") currently throws an exception where what() only outputs "stoi"
> (nothing else).

The type of the exception is significant too. It can either throw std::invalid_argument("stoi") or std::out_of_range("stoi").


> Some component (either __stoa() or __throw_invalid_argument())
> should extend the message at least a bit.
> E.g. __stoa() might call
>        std::__throw_invalid_argument("invalid argument for " + __name)

This makes it (very slightly) more likely to throw std::bad_alloc than to throw what we actually want to throw. It's probably a reasonable trade-off to get a better what() message though.