3 ms·
This is only because you use exceptions incorrectly. You can (and should) write: try { final var fResult = f(g()); //do something with fResult } catch
by mkleczek 3y ago
This is only because you use exceptions incorrectly. You can (and should) write:
try {
final var fResult = f(g());
//do something with fResult
}
catch (E1 e) {...}
catch (En e) {...}
That's the main idea of exceptions in all languages: main flow is kept together and exceptional flows are separate.
- im3w1l 3y agoI think the issue with this pattern is that either we don't care about went wrong, we want to treat all errors the same way, and then having n catch clauses is overkill. Or we do care about went wrong, and in that case the exception type is not enough to identify the issue, as often multiple lines in the try block will throw the same exception type.
- mkleczek 3y agoYes - that's one of the issues with structured exception handling. Relying only on exception type itself is not enough as information is lost (ie. the actual source of exception)
- im3w1l 3y agoCrazy idea: Maybe it would be helpful if we could label operations, and catch by either label or type or (label, type). The we could centralize error handling without losing anything.
- mkleczek 3y agoWhile deceptive at first... Just try to imagine how "extract method" refactoring would work...
- im3w1l 3y agoIt would have to do some clever rewriting of the logic or it could simply declare that extracting method isn't available on that region. The simplest way to rewrite the logic may be the introduction of new exception type(s) corresponding to the labels.
- AlphaSite 3y agoI think there’s a strong assumption in this pattern that can and should be handled immediately and that there is a recovery path from the failure, if there is no recovery path and you’re just propagating the error this pattern becomes an awfully verbose return statement.
- mkleczek 3y agoWhen structured exception handling was becoming popular (C++ in nineties?) the idea was that immediate error handling (like when you have to check return values for errors) makes the main flow (happy path) blurred and unreadable. You cannot have both at the same time, I'm afraid.