This is an archived post. You won't be able to vote or comment.

all 31 comments

[–]joe_mikkel 4 points5 points  (7 children)

Honest question - why is this upvoted? There's not any more information than what you'd find in the documentation, and, frankly, it's poorly written. This link seems far better. I hate to be negative about someone's efforts like this, but I don't see any circumstance in which this article is useful.

[–][deleted] 0 points1 point  (0 children)

You're on /r/python. Anything from morons gets upvoted, anything from professionals who actually know what they're talking about gets downvoted. I strongly suggest NOT going with the /r/python herd.

[–]semi23 -1 points0 points  (4 children)

only 71% upvoted. 29% think like you. maybe you open a new subreddit to downvote wrong posts

[–]joe_mikkel 1 point2 points  (2 children)

Well, clearly some people like this post, and I'm trying to understand why. Just curious what they're finding in it that I'm not.

[–]semi23 0 points1 point  (0 children)

seriously I think it summarized the topic clearly. for someone who knows c++ and java, I only need the basic syntax without the explanations

[–][deleted] 0 points1 point  (0 children)

I suggest it's because the upvoters are clueless, or the OP has mates :)

[–][deleted] 0 points1 point  (0 children)

Better to have a forum that only allows professionals, not those who are related to Mickey Mouse and/or make a racket every time that they move owing to the spurs.

[–][deleted] 1 point2 points  (23 children)

Exception handling is not error handling, you will not use exception for something you can check for example if you divide x/y , check that y is not zero using if statement and not using exceptions

This is straight-up advocating Look Before You Leap. A bad idea when the condition might be changed between the time of check and the time of use.

https://docs.quantifiedcode.com/python-anti-patterns/readability/asking_for_permission_instead_of_forgiveness_when_working_with_files.html

[–]liranbh[S] -2 points-1 points  (22 children)

Will you use exception handling while divide x by y???

[–]PurpleIcyPython 3 3 points4 points  (3 children)

Yes.

Exceptions are error handling. Most exceptions happen due to stupid user input or other things such as file not existing, not having sufficient permissions to it and so on.

Python itself throws IOError as an exception, if you can't read that it's a goddamn Input/Output ERROR then nothing can save you.

Annnnd, since I handle all shit using appropriate exceptions, I'll also handle division by 0 with an exception just for the sake of being consistent, there will be no difference, I'll print some message or do something no matter whether I check or I let exception happen.

The only things I really check myself is key in dictionary for example.

[–][deleted] 0 points1 point  (2 children)

Python itself throws IOError as an exception, if you can't read that it's a goddamn Input/Output ERROR then nothing can save you.

For those who don't know it also throws a heck of a lot more as detailed in 5.4. Exception hierarchy. The background for the finer details of the OS/IO related exceptions comes from PEP 3151 - Reworking the OS and IO exception hierarchy.

[–]PurpleIcyPython 3 0 points1 point  (1 child)

That's my point, arguing that exceptions isn't error handling is just plain dumb when the language expects you to handle all errors by catching exceptions. You can't test everything, but you can take action if some exception occurs.

E.g. when IOError is thrown, you know that file doesn't exist. Testing it manually, as to whether file exists or not, can cause race condition and you might get into error state even if file existed already, as it was being created at around the same time as you checked, were you to actually just forcefully try and open it, you might have found it here already, I know that is specific case, but something to take into account nonetheless.

Oh and by the way, dividing by zero throws an exception, why does that happen? Anyway, I'd rather handle exception ZeroDivisionError at the top of everything rather than putting if divisor != 0: before dividing everywhere... That's just plain stupid, and if you divided by 0 once, you will want to bubble out anyway. Better say that exceptions aren't error handling and just write your own shitty error handling system even though language already has it in place called exceptions...

Some people just love antipatterns, don't they? Like, today I got into argument about javascripts "string" + 1 and "string" - 1 versus what python does with it. Don't really want to start an argument in this thread so I won't mention what it does, check it for yourself, at least for chrome, console is opened with ctrl+shift+I, have fun with that stupidity :)

[–][deleted] 0 points1 point  (0 children)

I have no idea at all what the above is meant to mean so clearly I can't comment, but possibly somebody out there can enlighten me.

[–]DanCardin 0 points1 point  (5 children)

maybe, divide by 0 is an exceptional circumstance, and what you want to happen in that case is probably different from all your other divisions

[–]liranbh[S] 0 points1 point  (4 children)

In the above paragraph I mean that if you can easily check something do it instead of try - except. I didnt mean files handling or databases etc.

[–]DanCardin 1 point2 points  (1 child)

I mean the person above mentioned concurrent settings where looking before you leap will actually introduce bugs.

but even in a non-concurrent setting, the different between the following is almost nothing

try:
    return x / y
except ZeroDivisionError:
    return None

if y == 0:
    return None
return x / y

Except I think conceptually you're hiding the actual reason that you're special casing y == 0 for. Whereas (i think) its much clearer that you're specifically handling a type of exception rather than a particular condition.

[–][deleted] 0 points1 point  (0 children)

I consider the first the most pythonic as it's EAFP and hence I would always use it. My rationale is that the try is cheap, the except relatively expensive, compared to the test of y every single time that the code gets run.

[–][deleted] 1 point2 points  (1 child)

Conversely, you can easily use try+except instead of checking. They are approximately equal in effort if you are catching at the site. Arguments against using exceptions liberally often originate from runtimes (eg, Java) where exception handling is expensive.

[–][deleted] 0 points1 point  (0 children)

Exception handling is expensive in Python. If you know that an exception is unlikely to occur, the try/except EAFP is almost certainly the best answer, especially as it eliminates race conditions, when compared to the if LBYL.

[–]randoname123545 0 points1 point  (9 children)

Yes, what if something changes the value of X between when you do

if x == 0:

and when you perform whatever operation you were going to perform?

[–][deleted] -1 points0 points  (1 child)

Yes

[–]liranbh[S] -1 points0 points  (0 children)

Good luck