this post was submitted on 13 Sep 2024
61 points (86.7% liked)
Programming
17537 readers
129 users here now
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Rules
- Follow the programming.dev instance rules
- Keep content related to programming in some way
- If you're posting long videos try to add in some form of tldr for those who don't want to watch videos
Wormhole
Follow the wormhole through a path of communities [email protected]
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
I think I disagree with everything here.
Well, they're "easier" in the same way that dynamic typing is easier. It's obviously less work initially just to say "screw it; any error gets caught in
main()
". But that's short term easiness. In the long term its much more painful because:(It actually gets worse than that but I can't think of a good example.)
Well... I'm guessing your codebase is a lot smaller than the other one for a start, and you're comparing with Go which is kind of worst case... But anyway this kind of proves my point! You only actually have proper error handling in 140 places - apparently mostly in tests. In other words you just throw all exceptions to
main()
.Kind of a fair point I guess. I dunno how you can reasonably stack overflows without exceptions. But guess what - Rust does have
panic!()
for that, and you can catch panics. I'd say that's one of the few reasonable cases to usecatch_unwind
.Hahahahahaha. I dunno if a bare stack trace with
NullPointerException
counts as a "better error message". Ridiculous.Sure maybe in error handling microbenchmarks, or particularly extreme examples. In real world code it clearly makes little difference. Certainly not enough to choose an inferior error handling system.
I would say one real reason to prefer exceptions over
Result<>
s is they are a fair bit easier to debug because you can just break on throw. That's tricky withResult<>
because creating aErr
is not necessarily an error. At least I have not found a way to "break onErr
". You can break onunwrap()
but that is usually after the stack has been unwound quite a bit and you lose all context.There's also a massive tradeoff for when the error condition actually occurs. If an exception does get thrown and caught, that is comparatively slowwww.
The author pointed out how exceptions are often faster than checking every value. If your functions throws an error often enough that Exception handling noticeably slow down your program, surely you got to take a second look at what you're doing.
It depends what kind of errors you're talking about. Suppose you're implementing retries in a network protocol. You can get errors pretty regularly, and the error handling will be a nontrivial amount of your runtime.