I believe at some point C# didnt even guarantee the first item added was the first iterated. Might have changed though 🤷 If not, a random exception is thrown...
It makes sense if you want try every batch and deal with the errors after. But I do appreciate throwing errors in a foreach will only use the first one
Ok, I get that, but why assume that this exception is unhandled? This piece of code could be handy for debugging if it just prints or logs all the errors, instead of only one it encounters.
throw means that the control flow skips to wherever the exception is handled. In terms of control flow it's a little like sticking a return statement in the for loop, the first return interrupts the rest of the loop. The code never gets to throwing the second exception in that list.
I like how everybody is still using goto but we call it throw now and it’s a little worse because there’s not even a label / line to know where its going to get handled.
Anyway, whoever wrote this probably wanted whatever Generators / coroutines / channels are called in C#, and was thinking control flow would return here after (or concurrently with when) an upstream handler took care of the exception.
All control flow can be seen as constrained goto. Exceptions are just another category of that. What exceptions and if statements and loops have and goto doesn't is semantics: you can tell what the intent is just by looking at whether you're using an if statement, a loop, or throwing an exception.
Also the point of exceptions is that your code shouldn't depend on where they are handled. The semantics are "I'm not dealing with this, if someone else wants to, they're welcome to try."
That’s so handliscist, just cause you can’t see a try catch doesn’t mean OP isn’t running a custom compiler that automatically wraps exceptions being thrown within loops
The exceptions don’t occur in a nested or recursive stack, they occur during iterations over a collection. So nesting the exceptions would grossly misrepresent what’s going on here.
Ohhh WTH! 😮💀 1st of all, there is no way to throw multiple exceptions and still executing that method lol. At the first exception the execution is interrupted. Handle the error into the catch block, clean up in the finally block. Or just re-throw the exemption for the consumer of that method to deal with. That’s some fuckery right there. I hope that came from a junior 😂😂😂
No you couldn’t. There is no defense for trying to loop over exceptions and throw each one. That’s like putting an unconditional return statement inside the loop. It makes no sense.
My defense is that if you know how it behaves and you want it to behave that way, there is no simpler way to write it. Checking if the list is non-empty and throwing the first one is more complicated.
My defense is that it does what I want it to do, it throws the first exception in the list and I’m able to log the entire list…. Lol. Or some shit. Idk. I just like how serious everyone is, like holy crap chill.
I know what’s wrong with the code. What if his intention was to always iterate threw the whole list but only throw the first exception after iteration?
This does that.
Could he do a if count > 0 throw list[0] yeah but honestly we are talking about a very minor different in performance. And if you changed the code from throw exception to log exception, it works fine.
Is it right. No. Is it bad. Yes. But it’s more fun to defend bad code than to just say “this isn’t right”
If the non-thrown exceptions were logged , I think it’s close to passable. An exception group or similar would be better but I don’t know this language.
It’s ugly but it’s better than breaking due to not handling an error
196
u/MerlinSmitty Feb 06 '23 edited Feb 06 '23
(Attempted) Throw all the things!
175
u/IHeartBadCode Feb 06 '23
In Java we call this an ExceptionFactory.
128
u/dav1d_23 Feb 06 '23
First come first thrown
41
u/Eyes_and_teeth Feb 06 '23
Pokémon Exception Handling: Gotta catch them all!
145
u/NikZM Feb 06 '23
It makes sense if you want try every batch and deal with the errors after. But I do appreciate throwing errors in a foreach will only use the first one
35
u/no-lewding Feb 06 '23
Oh thank god
16
u/stopleaksfast Feb 06 '23
Result monad at home:
31
u/Bisquizzle Feb 06 '23
What am i even looking at? Why throw exceptions in a for each loop that is not going to work. Ever heard of AggregateException??
18
u/CodeByNumbers Feb 07 '23
Clearly we need a
yield throwlanguage feature, now.8
u/ign1fy Feb 06 '23
I'm now imagining that crazy cat lady from the Simpsons, but throwing exceptions at you instead of cats.
4
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Feb 06 '23
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
4
u/rexspook Feb 06 '23
The bad part of throwing them in a loop. AggregateException class exists for a reason and would make this acceptable code depending on the use case.
3
u/timbar1234 Feb 06 '23
I am maybe sad that Aggregate Exception doesn't appear here.
3
u/The_Northern_Light Feb 06 '23
internal screaming intensifies
3
u/TheRealSlimCoder Feb 07 '23
Do now, bitch later….. kind of like it
3
u/micro-amnesia Feb 07 '23
The exception participation trophy
2
u/gsckoco Feb 06 '23
I can see a use of capturing all errors and storing them for later, but trying to throw them all at the end is just such an insane idea
2
u/ensoniq2k Feb 06 '23 edited Feb 07 '23
Real pros just nest all those exceptions inside each other and then throw the topmost one. Still ridiculous though.
2
u/[deleted] Feb 08 '23
Its rare that code makes me laugh but this exception vomit is hilarious
1
u/ganzefolge Feb 07 '23
I mean, at least it collects all the exceptions
1
u/propostor Feb 07 '23
0 references, 0 changes, 0 authors.
So you wrote this yourself for Reddit points?
Edit: Never mind, I see in another comment you said you extracted it from a larger code base for clarity. Excellent badcode discovery!
1
u/Mean_Internet2805 Feb 06 '23
that foreach for the exceptions will only work for the first one.
1
u/Extra_Initiative_822 Feb 07 '23
Nice
1
u/Cybasura Feb 07 '23
I see what they are doing
They are trying to get through every item, 1-by-1, and check of is valid
Something like a filter system, but there's better ways im sure
1
u/NickSicilianu Feb 07 '23
Ohhh WTH! 😮💀 1st of all, there is no way to throw multiple exceptions and still executing that method lol. At the first exception the execution is interrupted. Handle the error into the catch block, clean up in the finally block. Or just re-throw the exemption for the consumer of that method to deal with. That’s some fuckery right there. I hope that came from a junior 😂😂😂
1
u/SolarSalsa Feb 07 '23
Elon would love this due to the extra lines of code. More code = better code!
1
u/[deleted] Feb 08 '23
Lmfao
1
u/Aperture_Executive2 Feb 08 '23
“If you fail at something, do it harder”
“Boss, you cannot throw an exception harder.”
“Did i stutter?”
1
u/[deleted] Feb 08 '23
Why every curly brace is on a new row? Is it something with C#? Me personaly tickles me a lot, 1 function should fit on your 19 inch screen lol
1
u/leeeeny Feb 09 '23
They forgot to add another try/catch in the foreach loop
0
u/veryusedrname Feb 06 '23
Well, it's fine, 0 references
0
u/WisestAirBender Feb 06 '23
Aren't you supposed to do something similar if using async await?
-5
u/Rott3Y Feb 06 '23
Honestly. I could defend this shitty code in a code review.
246
u/[deleted] Feb 06 '23
[removed] — view removed comment