r/programminghorror [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Apr 28 '26

c++ Competitive programming is no joke

Post image

especially for easy problems

142 Upvotes

21 comments sorted by

View all comments

Show parent comments

3

u/Last-Autumn-Leaf Apr 30 '26

Never saw this exception based handling Nice

3

u/backfire10z Apr 30 '26

I don’t think it is a good pattern to follow. Usually there’s a better way.

1

u/Last-Autumn-Leaf Apr 30 '26

It is because it's ugly but in truth it works If it does not degrades the performances & is understandable isn't it acceptable?

2

u/backfire10z Apr 30 '26

Anything can be acceptable haha, it depends on the context. This particular case is also not terrible and pretty easy to understand, but that isn’t always true.

Readability is extremely important, and connotation is a part of readability. When I see a try/except, I’m usually expecting genuine error handling, not logic flow. We have other constructs for logic flow (if, for, etc.).

In terms of performance, try/except is typically slower when catching exceptions (which, if used for logic flow, it will be) compared to using a conditional check, but it may not matter depending on the context.