TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: doesn't runtime exceptions and unit tests share the same goal?

1 pointsby highCsalmost 9 years ago
I still see many programmers guarding runtime errors using "ifs". Which is ridiculous and leads to programs very hard to debug. Which then kind-of leads to the use (and importance) of unit tests. Any thought on that?

5 comments

viraptoralmost 9 years ago
&gt; Which is ridiculous and leads to programs very hard to debug<p>What&#x27;s the alternative in your opinion? Let&#x27;s say you&#x27;re trying to open a file. It can fail. You&#x27;ve got to check this.
gt2almost 9 years ago
Couldn&#x27;t the ifs have other purpose, such as branching to perform other operations in those cases?<p>Also, consider that if the entire block isn&#x27;t wrapped in a try&#x2F;catch, those ifs are guarding against runtime errors which would crash the program for the user.<p>Although I do see unit tests making use of exceptions in their decision to pass&#x2F;fail, it&#x27;s not the only way to write a unit test-- sometimes you test for value, etc.
bbcbasicalmost 9 years ago
Can you give an example and how you&#x27;d re factor it. I&#x27;m having a hard time visualising what you mean.<p>I have seen code that catches any exception and returns false. That is annoying although for different reasons.
GFK_of_xmaspastalmost 9 years ago
How does the existence of a unit test suite, no matter how good, preclude against a runtime error?
liveoneggsalmost 9 years ago
You sound like a great candidate for Erlang!