4 ms·
Yes, I see the reasoning now. But isn't this pure syntactic sugar? I.e. if exceptions weren't costly, a good way to implement TryParse() would be to simply call
by Mikhail_Edoshin 7y ago
Yes, I see the reasoning now. But isn't this pure syntactic sugar? I.e. if exceptions weren't costly, a good way to implement TryParse() would be to simply call Parse(), catch the exception and dress it up as a boolean.
- kllrnohj 7y agoIf exceptions were free then yes, but there's not really any language/platform where that's the case. It'd instead be the reverse - Parse() calls TryParse() and throws an exception on false.
- Mikhail_Edoshin 7y agoA reverse approach would lose exception details; you cannot recover what exactly happened from a mere boolean and you may need it. It may not be possible to make exceptions totally free, but the rest of the language may be slow enough so they won't stick out :) Example: Python.
- ncmncm 7y agoTypically "try_parse" returns more than a bool on failure. What it does return is then wrapped and thrown.