Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Not two systems, two APIs; like Int32.Parse(String): Int32 and Int32.TryParse(String, out Int32): Boolean in .NET.

It's quite rare to want to handle an error condition half-way up the stack and do anything more than log / cleanup and resume unwinding (whether implicitly via exceptions or monadic errors or automatically via exception unwinding library).

You normally handle errors like file not found, couldn't connect, etc. near the leaf call which failed, if you want to handle them at all. So it makes sense to decide what error condition you want; do you abort the whole task (exception works great!), or do you do something interesting like retries or alternatives (exceptions not so hot, booleans or some kind of error code better reflects that the handler code is conditional rather than exceptional).



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.


If 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.


A 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.


Typically "try_parse" returns more than a bool on failure. What it does return is then wrapped and thrown.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: