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.

The Ultimate Guide to Error Handling in Python

96 pointsby kdamica7 months ago

5 comments

syllogism7 months ago
The examples in the article recommend doing the right things, but I think the initial discussion of the LBYL vs EAFP discussion could be better.<p>The article says basically &quot;LBYL is bad&quot;, but this isn&#x27;t a good description of what the author does in the later examples. Following &quot;EAFP&quot; without exception is also really bad.<p>The simple policy is that you should use &quot;LBYL&quot; when you&#x27;re dealing with local state that can&#x27;t change out from under you, especially when it&#x27;s just properties of your local variables, but you need to use &quot;EAFP&quot; for anything that deals with remote state. Function calls can go either way, depending.<p>Blanket advice against LBYL leads people to use try&#x2F;except blocks to express basic conditionals. If you want to ask something about your local variables, just ask it in a conditional --- don&#x27;t make the reader infer &quot;ah, these are the situations which will raise the error, and therefore we&#x27;ll go into this block if the values are this way&quot;. If you want to do something when a key is missing from a dictionary, don&#x27;t write:<p><pre><code> try: value = table[key] except KeyError: return default_value </code></pre> Just write:<p><pre><code> if key not in table: return default_value value = table[key] </code></pre> (If you really need to avoid two lookups for efficiency, you would do something like value = table.get(key, MISSING_VALUE) and then check &#x27;if value is missing_value&#x27;. But this optimisation will seldom be necessary.)<p>The example the author gives about interacting with the file system is a good example of where indeed you really should use EAFP. The file system is remote state that your function does not own. Similarly if you&#x27;re doing database operations, calling a remote API...lots of things.<p>There&#x27;s various middle ground when you&#x27;re calling functions. Often you don&#x27;t want to worry about whether that function is going to be interacting with remote state, and you want to treat it totally as a black box, so you just use EAFP. But if the function documents really clear pre-conditions of your variables you can just go ahead and check, it&#x27;s better to do that.
评论 #41797660 未加载
评论 #41798759 未加载
alex_suzuki7 months ago
One thing that bothers me in my day-to-day Python work is that often libraries don’t document the errors that can be thrown by their functions. As the language lacks a “throws” statement, I found myself digging through library code on multiple occasions. How do people approach this?
评论 #41800494 未加载
评论 #41797728 未加载
atoav7 months ago
You can do nearly everything using Python errors. One thing that bothers me a bit, is the amount of text needed to catch them and bubble them up. Depending on the programming style traditional exceptions may also not be adequately re<p>For this Rust style error handling can be interesting, eg using poltergeist (<a href="https:&#x2F;&#x2F;github.com&#x2F;alexandermalyga&#x2F;poltergeist">https:&#x2F;&#x2F;github.com&#x2F;alexandermalyga&#x2F;poltergeist</a>) or result (<a href="https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;result&#x2F;" rel="nofollow">https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;result&#x2F;</a>).
评论 #41796968 未加载
valbaca7 months ago
Can someone explain the last bit:<p><pre><code> print(f&quot;Unexpected error: {error}&quot;) sys.exit(1) </code></pre> How is that any different than just letting the exception throw all the way out?<p>Is all this doing is making sure the exit code is 1 instead of some other non-zero value?<p>I&#x27;m not seeing how this is any better than just letting the program hard-crash.<p>(I am asking in earnest. I&#x27;m getting back into Python and wanting to know what&#x27;s generally preferred)
评论 #41800993 未加载
评论 #41801000 未加载
InDubioProRubio7 months ago
It still has exceptions- but exceptions are normal. Errors are normal. How would one do : <a href="https:&#x2F;&#x2F;fsharpforfunandprofit.com&#x2F;rop&#x2F;" rel="nofollow">https:&#x2F;&#x2F;fsharpforfunandprofit.com&#x2F;rop&#x2F;</a> in python?
评论 #41797417 未加载
评论 #41797012 未加载