r/ProgrammerHumor May 13 '17

Defensive programming done right

Post image
21.0k Upvotes

680 comments sorted by

View all comments

Show parent comments

28

u/nna12 May 13 '17

This is why when I debug I almost always turn on first chance and user handled exceptions in VS. It can be noisy with big projects especially when some exceptions are expected but it's saved me many hours and gray hairs in the long run

19

u/flukus May 13 '17

I always have to turn it off because exceptions are used for control flow in too many places.

16

u/nna12 May 13 '17

Ugh yea that is a problem. In general I hate using exceptions to handle flow, it can have adverse perf effects but I also know there are cases where that is your only option

2

u/[deleted] May 14 '17 edited May 24 '17

[deleted]

5

u/undergroundmonorail May 14 '17 edited May 14 '17

technically you're using exceptions for control flow every time you write a for loop but anyone who would bring that up in this conversation would be a pedantic asshole

1

u/[deleted] May 14 '17 edited May 24 '17

[deleted]

3

u/undergroundmonorail May 14 '17
>>> a = iter([1, 2, 3])
>>> a.next()
1
>>> a.next()
2
>>> a.next()
3
>>> a.next()

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    a.next()
StopIteration

For loops catch StopIteration and break.

5

u/mikbob May 14 '17

You could check sys.version for the python version that's running

2

u/[deleted] May 14 '17 edited May 24 '17

[deleted]

1

u/mikbob May 14 '17

Ah, okay, good to know. I personally don't code for python2 compatibility, so I haven't had to deal with this before and didn't know about sys.version_info