4 ms·
With exceptions being classes, a library designer can vastly simplify it for the users by creating useful class hierarchies for the exceptions, allowing the use
by teddyh 8y ago
With exceptions being classes, a library designer can vastly simplify it for the users by creating useful class hierarchies for the exceptions, allowing the user to be as specific or generic as they wish or need.
- rumcajz 8y agoThat's the theory. In reality each layer just passes exceptions from lower layers on, leaving the end user with a mess that cannot be handled in any sane way.
- he0001 8y agoTo me that’s an indication that those lower layers aren’t lower layers.
- hliyan 8y agoI've been writing code for 15 years (in Java, C++ and more recently JavaScript and Go). I have finally given up on exceptions and settled on return values (I particularly like Go's system). Exceptions, while theoretically superior, simply tempts even good programmers to just kick errors down the callstack. I prefer guard clauses [1]. [1] http://wiki.c2.com/?GuardClause http://wiki.c2.com/?GuardClause
- dtech 8y agoI like the functional approach with Try, basically the exception becomes part of the return type, and the code chooses to either return an exception or the actual value. fun doSomething(arg: X): Try<Y> Since the return signature needs adjusting, this leads to developers very consciously making the choice to either handle the error in the function, therefore avoiding adjusting the return type, or let the caller deal with it if it isn't logical to handle the error there.
- wool_gather 8y agoIs this the same as `Result<Y>` (where `Result` is a sum type that contains either a value of `Y` or an "Error")? I haven't see it called `Try` before.
- dtech 8y agoIt's a common name for the concept in Java, Scala, and to a lesser extend Javascript land. But it's just a name for a `T | Exception` type.
- scarface74 8y agoWhy wouldn’t you want errors kicked down the call stack? There are only two types of exceptions in the grand scheme of things. 1. Things go wrong that are out of your control - network down, database down, etc. 2. Coding mistakes. Either in your code or input arguments. In either case, why not let the end user decide how to handle the error? Sometimes it’s some type of retry pattern, others it just to have a big try catch block that logs the fatal error and alerts someone.
- hliyan 8y ago> network down, database down These should be handled gracefully at each layer, and an appropriate error thrown to the layer above.
- scarface74 8y agoAt the highest layer. I don’t want other layers to wrap the error and probably lose the original error and the stack trace. If the system is database or network dependent, there is no graceful way to handle it automatically most of the time.
- teddyh 8y ago> probably lose the original error and the stack trace. In particular, Python solves this by having a “raise … from” language construct: try: dangerous_operation() except DangerousException as e: raise ThisModulesException("Dangerous operation failed") from e Then, the original exception (including its stack trace, etc.) is available as an attribute on the exception you caught.
- scarface74 8y agoI’m not really in love with that either. The equivalent paradigm in C# is using the “Inner Exception” what value did you add by wrapping the exception? The stack trace already has the line number and the method that caused the error all the way down the stack. In Python, the code is right there. You can just open up the Python file in a text editor and see everything.