3 ms·
I think it's actually worse than exception-based error handling, as exceptions are typically integrated in the runtime in such a way that they produce a nice st
by tsimionescu 4y ago
I think it's actually worse than exception-based error handling, as exceptions are typically integrated in the runtime in such a way that they produce a nice stack trace, which is very very often all the context you actually need (though sometimes parameter/variable values are important as well). The Result/Either/Error/[...] types have no such special support, so they often indeed just give you a single error type and no other context.
On the other hand, it's important to note that, except for maybe adding context and bubbling up, the vast majority of code in any code base will not actually handle errors, so the advantage of taking away the huge amounts of boilerplate when possible are still worth in my experience (as someone who has ~7 years of Java experience and have moved to Go for 3 years, all in a commercial setting with a large-ish team).
- valenterry 4y agoWhat does make people always think that the two things don't go together? In Scala I use Exceptions, but I don't throw them, I use monadic composition style. So I end up with clean code AND a stacktrace, just as if I had thrown them. Sometimes I wrap them for even easier debugging, e.g. for { user <- getUser message <- getMessage } yield showMessage(message, user) And then when I call this function I do renderMessage(x).mapError(e => RenderMessageError(message = x, cause = e)) Which, in case of an error, gives me a typical jvm stacktrace Exception in thread "main" com.myproject.module.MyProjectFooBarException: Message x could not be rendered at com.myproject.module.MyProject.anotherMethod(MyProject.java:19) at com.myproject.module.MyProject.someMethod(MyProject.java:12) at com.myproject.module.MyProject.main(MyProject.java:8) Caused by: DatabaseError: User for ID ... not found at org...(Fraction.java:143) at com....(MyProject.java:17) ... 2 more
- tsimionescu 4y agoI'm not familiar with Scala or its integration with the JVM, but in Java at least, a stack trace is only collected for an exception from the moment it is thrown. If you create an exception object in one function, return it as a simple value, and then throw it from a new place, the stack trace will not show when the exception was created, only when it was thrown. This would mean that, in your example, if getUser were returning an error, the stack trace wouldn't show that, it would only point to the mapError() function. Is this correct, or does Scala actually integrate with the JVM such that it can show the original source of the error?
- valenterry 4y agoI don't think what you say is correct. See: https://stackoverflow.com/questions/69065097/why-java-stack-traces-are-generated-when-exceptions-are-constructed https://stackoverflow.com/questions/69065097/why-java-stack-... Hence it works in Scala (and also Java) without throwing Edit: maybe what you meant to say is that the stack is only unwinded at the time of generating the stacktrace and not when the exception is created? Which is true and a good thing since it's expensive to create it.
- tsimionescu 4y agoYou're right, andy impression was wrong. Thanks for finding out the correct info!