3 ms·
> If you're wrapping a try block around every piece of code that might throw an exception, then you're doing it wrong. Well then the C++ standard people are do
by IshKebab 10y ago
> If you're wrapping a try block around every piece of code that might throw an exception, then you're doing it wrong.
Well then the C++ standard people are doing it wrong, because something as expected-to-fail as converting a string to a number throws an exception.
What am I supposed to do if I want to load a text file containing numbers and print out where it failed? Yep, I have to wrap every string-to-number conversion in a try-catch block.
Or in reality I make a wrapper function that does it. This is why exceptions are stupid - 80% of the time you want the context provided by the program counter in order to handle the error.
int string_to_int(const std::string& s, int def)
{
try
{
size_t pos = 0;
int v = std::stoi(s, &pos);
// Check that the number was parsed from the entire string (stoi ignores trailing garbage).
if (pos == s.size())
return v;
}
catch (std::logic_error&)
{
// This occurs if there is no conversion (e.g. no digits) or the number doesn't fit in an int.
}
return def;
}
- qznc 10y agoWrite a proper lexer and parser. You are half-way there already.
- Suncho 10y ago> Well then the C++ standard people are doing it wrong, because something as expected-to-fail as converting a string to a number throws an exception. I wouldn't say that they are doing it wrong. They throw an exception because they don't want to make assumptions about how you would want to handle the error. What would you prefer the std::stoi() function to do?
- IshKebab 10y agoEither allow you to specify a default value on failure, or take an optional `bool* success = nullptr` parameter to indicate failure. Or in C++17, return `std::optional<int>`.
- Suncho 10y agoThen std::atoi() would be making the assumption that you want to handle the error as soon as the function call returns. Furthermore, if you don't check the "success" flag, then your function would fail silently. The exception ensures that something reasonable happens (program termination) when the client does nothing to handle the error. Exceptions are cleaner.
- reacweb 10y agoexception shall occur in exceptional circumstances because they are costly in C++. A corrupted data file is a good case. exceptions in C++ shall not be used to handle and end_of_file. This programming style is very specific to the language. In another language, exception may be used more widely.
- _pmf_ 10y ago> exception shall occur in exceptional circumstances because they are costly in C++ The upside is that the happy path (without exceptions) can be branch-free (or at least contain fewer branches than equivalent code handling errors), which is beneficial to performance (hot path stays hot).
- HelloNurse 10y agoConverting a string to a number throws an exception because either you are sure it's a number and an exception is an elegant way for your program to die in case there's a bug or an exceptional condition; or you aren't sure and you need to validate the input and deal with bad inputs on your own before attempting the conversion.
- atemerev 10y agoBecause functional programming ideas were not as common when the C++ standard was defined. Back then, exceptions were all hip, and all the cool kids on the block played with them.
- kazinator 10y ago> Yep, I have to wrap every string-to-number conversion in a try-catch block. Ah, that's simply because C++ exceptions are unwind-only, non-restartable. "Blub" exceptions. > This is why exceptions are stupid - 80% of the time you want the context provided by the program counter. Exactly. Imagine if a CPU exception threw away that context. We couldn't implement virtual memory, which requires faulting instructions to be precisely restarted.
- AstralStorm 10y agoGenerally the context is held by most implementations (And accessible by implementation specific tricks), but that is not required by the standard as some platforms have highly limited stack Or other storage for such data. Why would you want to restart same code after an exception anyway? Conditions have changed...