Exception handling is hard
-
@cartman82 Bear in mind that in C#,
throw ex
means they're trashing the stack trace so all you'll know is that something in thattry
threw the exception. If it was justthrow;
, it wouldn't be as bad as that would preserve the stack trace
-
@Jaloopa said in Exception handling is hard:
@cartman82 Bear in mind that in C#, throw ex means they're trashing the stack trace so all you'll know is that something in that try threw the exception. If it was just throw;, it wouldn't be as bad as that would preserve the stack trace
Oh right. That was WTF-y. But the pattern itself is OK.
-
-
-
@Jaloopa clearly superior to do this way:
!condition && throw new ConditionFailedException("error message"); DoStuff();
-
@Jaloopa said in Exception handling is hard:
in C#
Couldn't tell by the fragment that it was C# and not Java. The syntax was valid in both (and not a WTF in Java, which builds stack traces on first throw only).
-
@dkf Wouldn't that mean that any given exception object can only ever be thrown (as opposed to rethrown) once?
-
@masonwheeler Yes. So what? Reusing the same exception in lots of different places (as opposed to sending it on its way) is just asking for confusion.
-
@dcon said stuff Exception handling is hard:
Oh god.
if ( function(x, y, z(y)) && anotherreallylongfunc( findmeinanotherworld(x) , findmeinanotherworld(x) ) && anotherfunc(withreallycomplexshit) && moreshit() && anotherreallylongfunc2( findmeinanotherworld(x) , findmeinanotherworld(x) ) ) { DoMe(); }
Also, you had a syntax error. Undisciplined coding does that.
-
@boomzilla said in Exception handling is hard:
your still wrong
-
@Captain You had to ruin it with that last line didn't you? How do you see where the logic is when scanning that?
-
@hungrier Hmm... Is that the myth about whether firing a handgun can explode a room full of methane?
-
@RaceProUK Myth: @RaceProUK watches too much Mythbusters
Status:
-
@Jaloopa "Logic" is a functor from the boolean algebra onto whatever monad you're in. (In other words, the if clause applies to the block like a function). That is why I prefer to indent the block to which the if-clause applies.
-
@Jaloopa Wait... I got that right?
Also,
@Jaloopa said in Exception handling is hard:too much Mythbusters
-
@RaceProUK said in Exception handling is hard:
Wait... I got that right?
I dunno, You sounded pretty confident, which suggests you know a lot about it.
@RaceProUK said in Exception handling is hard:
Meh. The early series were fun, before it got super formulaic and spent more time showing what was about to happen and what had just happened than actual mythbusting.
-
@RaceProUK said in Exception handling is hard:
Is that the myth about whether firing a handgun can explode a room full of methane?
It's a moonshine still exploding (in reply to "your still wrong"). I don't think there was a handgun involved in that one.
-
@Jaloopa said in Exception handling is hard:
The early series were fun, before it got super formulaic and spent more time showing what was about to happen and what had just happened than actual mythbusting.
I find that sort of thing happens to a lot of American shows.
@hungrier said in Exception handling is hard:
It's a moonshine still exploding (in reply to "your still wrong"). I don't think there was a handgun involved in that one.
Ah. So I've not watched too much yet.
-
-
@masonwheeler Because it's easier than the alternative.
-
@Captain If there's something that monads are easier then, I don't think I've ever heard of it...
-
I'm not surprised that you haven't heard of a name for the quagmire of incomplete concepts you like to program in.
-
@Captain ???
-
@Captain said in Exception handling is hard:
That is why I prefer to indent the block to which the if-clause applies.
and so does pretty much everyone else.... what they don't usually do, however, is indent the braces and run it all together on the same line.
-
It was all going great until you tried to DoMe.
I bet you've heard that one before though.if ( function(x, y, z(y)) && anotherreallylongfunc( findmeinanotherworld(x) , findmeinanotherworld(x) ) && anotherfunc(withreallycomplexshit) && moreshit() && anotherreallylongfunc2( findmeinanotherworld(x) , findmeinanotherworld(x) ) ) { DoMe(); }
*its hard to tell scope nesting indented code inside a quote markdown.... double scope
-
@darkmatter
actually, i'd probably do the if's parentheses like so though:if ( a bunch of crap to wrap the if conditional onto a lot of lines ) { DoMe(); }
or even
if ( a bunch of crap to wrap the if conditional onto a lot of lines ) { DoMe(); }
though in the case of the example where it's calling a bunch of functions that have their own ()'s, i'd probably go with the trailing ) on its own line to avoid confusing it with one of the functions
-
if ( Thing1() && thing2 && Thing3 && SomeOtherThing() ) { // CODE! }
Is ultimately what I would do, if I was stupid enough to and that many things in an
If
.
-
@Captain said in Exception handling is hard:
the quagmire of incomplete concepts
-
@dkf I think your engine may need some recalibration?
-
-
@darkmatter The block contained a single line of code. If it was multiple lines, I would have had a multiple line block.
-
I will also write
if( condition ) { DoMe(); }
rather than
if( condition ) DoMe;
or
if( condition ) { DoMe; }
But not if the condition doesn't fit on a single line.
-
@masonwheeler said in Exception handling is hard:
@boomzilla said in Exception handling is hard:
Treating whitespace as anything but a delimiter and where you merge consecutive ones
My brain threw an exception upon trying to parse that...
Let me clarify using braces
((Treating whitespace as anything but a delimiter) && (where you merge consecutive ones)) is dumber than (than a git command).
-
@dkf said in Exception handling is hard:
@Jaloopa said in Exception handling is hard:
(and not a WTF in Java, which builds stack traces on first throw only).On the Exception constructor, IIRC.
I... I might have written some code once which instantiated an exception for the sole purpose of stack trace inspection. >_>
-
@Zecc said in Exception handling is hard:
On the Exception constructor, IIRC.
It really depends on when the
fillInStackTrace()
method is called; that's effectively a one-shot.
-
@Zecc Those aren't braces; they're parens.
-
@PleegWat said in Exception handling is hard:
if( condition ) { DoMe(); }
Took a few tries before I stopped reading it as condom
-
@masonwheeler said in Exception handling is hard:
@Zecc Those aren't braces; they're parens.
Sorry, I was thinking about brackets, which presumably is a class also including parens.
-
@PleegWat said in Exception handling is hard:
I will also write
if( condition ) { DoMe(); }
rather than
if( condition ) DoMe;
or
if( condition ) { DoMe; }
But not if the condition doesn't fit on a single line.
I don't like
if( condition )
; I'd much rather seeif (condition)
which more closely resembles standard English syntax. And the only time I'll ever fail to use braces to turn a controlled statement into a controlled block is if both the condition and the controlled statement are short and clear enough that the whole if-statement fits on a single line, like so:if (a < MIN) a = MIN; if (a > MAX) a = MAX;
If the if-statement is clearer with separate lines for condition and controlled statement, I'll always brace the controlled statement using 1TBS:
if (slightest bit nontrivial condition) { deal with it; }
This reduces diff noise and opportunities for making syntactically correct mistakes if the controlled block ever gets statements added, purely because no braces ever need to be moved.
if( condition ) { DoMe(); }
has nothing at all going for it, in my opinion. The point of putting braces around a single controlled statement is to guard against subsequently added controlled statements accidentally becoming uncontrolled, and a single brace-wrapped statement on its own line is really not much help there. Sure, you can see the braces and they might remind you to be careful where you add your new statement(s), but the 1TBS version means that even if you fail to do that because the break room is out of coffee again and you're only paying attention to indenting, the thing is far more likely to end up correct than not.
-
@flabdablet said in Exception handling is hard:
if (a < MIN) a = MIN;
if (a > MAX) a = MAX;For small stuff like that, I prefer a ternary statement
a = a < MIN ? MIN : a; a = a > MAX ? MAX : a;
-
@Jaloopa That may be undesirable when using retarded embedded compilers or when
a
isvolatile
.
-
@Jaloopa Go hard or go home.
a = a < MIN? MIN: a > MAX? MAX: a;
-
@flabdablet I mean, if there's going to be a lot of forcing stuff to fit within bounds I'd factor it out and just have
a = FitToBounds(a, MIN, MAX);
-
@Jaloopa
Welcome to the wonderful* world of OpenCL. Batteries included**:*Not really.
**Even less true.
-
@Jaloopa said in Exception handling is hard:
@flabdablet I mean, if there's going to be a lot of forcing stuff to fit within bounds I'd factor it out and just have
a = FitToBounds(a, MIN, MAX);
I wouldn't. You'd have to go look at FitToBounds() to find out whether the bounds were inclusive or not, which order to specify them in and what it does if you specify a lower bound that's higher than the upper bound.
Also, the instant you made a FitToBounds(), some prick would stick it in npm and it would become a world-breaking dependency.
Simple logic like this is one of the very few places where copypasta works better.
-
-
@Jaloopa said in Exception handling is hard:
@flabdablet I mean, if there's going to be a lot of forcing stuff to fit within bounds I'd factor it out and just have
a = FitToBounds(a, MIN, MAX);
a = Max(MIN, Min(MAX, a))
If your standard library is non-retarded.
-
@loopback0 said in Exception handling is hard:
@boomzilla said in Exception handling is hard:
DOWNVOTED FOR:
DOWNVOTED FOR DOWNVOTING THAT
DOWNVOTED FOR LYING
-
@boomzilla said in Exception handling is hard:
@loopback0 said in Exception handling is hard:
@boomzilla said in Exception handling is hard:
DOWNVOTED FOR:
DOWNVOTED FOR DOWNVOTING THAT
DOWNVOTED FOR LYING
UPVOTED FOR NO REASON
-
@loopback0 said in Exception handling is hard:
@boomzilla said in Exception handling is hard:
DOWNVOTED FOR:
DOWNVOTED FOR DOWNVOTING THAT
Downvoted for downvoting that downvote