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

145 Upvotes

21 comments sorted by

View all comments

Show parent comment

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?

3

u/LivingVeterinarian47 May 01 '26

Exceptions can definitely degrade performance.

3

u/un_virus_SDF May 02 '26

Exceptions are really slow

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.

1

u/LivingVeterinarian47 May 01 '26

exceptions being caught in a closed loop is extremely costly speed wise, it can bring GUI to a stutter. In .NET a lot of the OS level stuff they handle with Exceptions. I think IO.File.Open() will throw 9 different exceptions. Exceptions are great for building tools for others to use, such as a framework or library, but we should really be abstracting them away when at all possible with real production code.

1

u/Last-Autumn-Leaf May 01 '26

Thank I did not know