3 ms·
TIL that it's even possible to use exceptions instead of bog standard if statements. Would love to know why people would do this, though. Surely everyone mast
by AussieWog93 4y ago
TIL that it's even possible to use exceptions instead of bog standard if statements.
Would love to know why people would do this, though. Surely everyone masters if-else ssatements well before they even learn what a try-catch statement is!?
- phist_mcgee 4y agoIf .. Else .. Finally?
- ajkjk 4y agoIf/else statements don't bubble and exceptions do. The code samples in this example are clearly simple representations of the pattern, but the pattern is to call into a more complex function than `get_positive_value`.
- shepherdjerred 4y agoUsing exceptions as control flow is a pattern I saw a lot at AWS in Java code. It boiled down to it was simpler to abuse exceptions. You could instead return objects, but the code simply ended up being more to write. At some point I tried to write things the 'proper' way by returning a Result object with the possible states, but it ended up being more complex than just throwing exceptions.
- erik_seaberg 4y agoIt’s because Java supports nonlocal returns and pattern matching, but only when you use exceptions. Returning POJOs to report problems makes all the intervening code harder to read because the happy path is no longer separated.