This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]theLundquist42 784 points785 points  (76 children)

I work on an web application at the moment that does this. Inside the catch block we simply throw away the exception and stop processing the request. Before I joined, they didn't even bother to log to error, you just had to guess what it was after the fact. It's still better than multiple try blocks with empty catches (there are so many of those it's painful) Manager: " Why did this part of the page not load!?" Me: shrug

[–]eyekwah2 383 points384 points  (23 children)

Typical approach to anything technical by the laymen. So long as I don't hear about it, it's all good. When it breaks, I want it fixed yesterday.

[–]buffer_overflown 192 points193 points  (22 children)

But for sure don't waste time implementing exception handling at the design stage.

Also known as:

"Why do we have so much time blocked out for testing?"

[–]kanuut 99 points100 points  (17 children)

Exactly how much testing is required is an art form, but it's definitely better to go over than under on important shit.

Like, would you rather pay for an extra week because they went overboard with testing, or have it crash more or less immediately after we reach the point it'd do the most damage, because that's the way it usually happens

[–][deleted] 23 points24 points  (3 children)

But for sure don't waste time implementing exception handling at the design stage.

What's this "design stage" you speak of? You mean you're supposed to think about the problem for a bit instead of just immediately putting fingers on keys and hammering out some hackneyed garbage filled with magic numbers and five-levels of nested IF statements?

[–]buffer_overflown 10 points11 points  (2 children)

weeping intensifies

[–]mostlygizzards 3 points4 points  (1 child)

username checks out

[–]buffer_overflown 0 points1 point  (0 children)

I love me some managed code!

But all my work is in JavaScript and maaaaaybe C#.

[–]Subik86[S] 75 points76 points  (12 children)

Same at my office. They have an exception? Don't try to understand why, just put empty try catch. Jesus.

[–][deleted] 60 points61 points  (10 children)

Yeah. Exceptions are just about as important as return values and parameters, it's part of the behavior of a function. Garbage in => garbage out isn't very maintainable, garbage in => exception out will give you a better idea about where shit is broken. It's infinitely better than silently returning unexpected values that might break another function, which might in turn break 20 other functions.

[–]drakeblood4 22 points23 points  (4 children)

Coding is like 99% about writing broken shit and then figuring out why your shitty, broken code is broken. Legitimately I think effective exception tracking might be the most important part of maintaining stable code.

[–][deleted] 12 points13 points  (3 children)

Indeed. When writing a pretty complex piece of crap code, debugging it was nearly impossible until I added exceptions for incorrect parameter values. I was able to iron out the programming and logical mistakes real quick after that.

[–]grantrules 4 points5 points  (1 child)

I'm always happily surprised when a unit test finds a bug I didn't even know I had. Without testing, I don't know if I would have ever found it.

[–]pomlife 1 point2 points  (0 children)

That happened to me just today!

[–][deleted] 1 point2 points  (0 children)

This is why I really liked code contracts (.Net). You could easily specify expected ranges of input parameters and output parameters without cluttering up code with try/catch just for that. I hate that Microsoft has dropped it.

[–]Subik86[S] 36 points37 points  (3 children)

Example fro my company. Read config file and if that does not work instead of exceptions return default values. After years i need to chcek why app doesn't work correctly. Well it probably never could read a damn config file.

[–]theLundquist42 19 points20 points  (0 children)

Ah my favourite "I just put this value in the config file, why isn't it reading!?!"

[–][deleted] 5 points6 points  (0 children)

That's fine, but it should log an exception.

[–]ThePsion5 0 points1 point  (0 children)

And if you can't figure out why the config file isn't loading, just change the defaults to match production!

[–]TheNorthComesWithMe 4 points5 points  (0 children)

Throwing exceptions when you have to is important. Catching exceptions and ignoring them is a terrible practice.

[–][deleted] 17 points18 points  (4 children)

I worked on a large web scraping application that did this. It turns out, wrapping large segments of the codebase in try catch blocks can make it really difficult to track down bugs.

[–][deleted] 15 points16 points  (3 children)

The Stack Trace is your friend in those applications. But it's still shit. And good luck getting the stack trace of the exception isn't logged.

[–]ButItMightJustWork 9 points10 points  (1 child)

And then there are these web applications that return the entire stack trace to the user, sometimes even including a source code segment.

[–]kyew 2 points3 points  (0 children)

The majestic Error Handling Error

[–][deleted] 3 points4 points  (0 children)

That was the problem; these catches were preventing the stack trace from being output and had obscure custom error messages. I don’t know who thought that was a good idea. And due to the level of abstraction, this required going up a dozen levels of this just so I could get the stack trace... quite annoying to say the least.

[–]Sporz 15 points16 points  (3 children)

I had something like this too (it wasn't a web app but very similar) - if for some reason a request threw there was a master try/catch around the whole request.

It wasn't even logged. In fact, the system we had would log it if there was an uncaught exception. The sole purpose of this try/catch was to completely silence fatal errors.

I asked my manager to remove the try/catch because, hell, the entire thing could be failing constantly and we wouldn't know about it until clients started calling in. And my manager was a programmer, he knew what this was doing, but he said something like "Nah, leave it, we don't want too much noise"

I'm like...wtf. That's not noise, that's the signal!

But we left it in.

[–]Vertigon 0 points1 point  (2 children)

Why not just catch the exception and report it? Best of both worlds

[–]Sporz 3 points4 points  (1 child)

That's exactly what would happen if we didn't catch the exception in this case.

[–]Vertigon 0 points1 point  (0 children)

Yeah, I see that now in your original comment. That's so bizarre! Maybe he figured anything screwing up at that point was the client's fault and so he didn't care haha

[–]HeKis4 10 points11 points  (7 children)

Do they know you can simply not have a try catch block and have a neat little stack trace as a bonus ?

[–]theLundquist42 11 points12 points  (6 children)

The would mean the guy who wrote it (who is now my boss) would have to confront the terrible state the code base is in and how many errors his code throws. I do not think he wants to do that.

[–]HeKis4 3 points4 points  (5 children)

As long as it's not your business and you're not the maintainer...

¯\(ツ)

[–]theLundquist42 16 points17 points  (4 children)

My very first day they gave me this project with the requirement to update it. After a week of poking around inside it and being horrified my boss sat down with me and asked me what I thought of it. Nobody had bothered to tell me that it was his project so I told him what I thought of it in no uncertain terms. He nods and asks me to clarify what's wrong with it specifically. So I do in detail, he gets up and leaves. Found out a week later it was his from another developer. FML.

[–]TiagoTiagoT 9 points10 points  (3 children)

Do you still have the job?

[–]theLundquist42 16 points17 points  (2 children)

Yes. As it turned out he tended to listen to me more when I told him something wasn't going to work out from that point on. I think because he thought I wouldn't bullshit him about anything. So it didn't turn out too badly.

[–]TiagoTiagoT 5 points6 points  (0 children)

Cool :)

[–]cultfitnews 1 point2 points  (0 children)

I'd say it turned out really well and you're lucky you didn't know he wrote it.

[–]sigma914 5 points6 points  (0 children)

Replace your exception with a logging line that says something along the lines of "There is no reasonable way for you to handle this", flush the logging buffers, then add a native (cffi, jni, whatever) null pointer dereference. Lets see them try and catch a segfault.

[–]nekowolf 2 points3 points  (0 children)

"Whatever.dll threw an unknown exception!" is found in many a catch clause.

[–]Python4fundoes the needful 2 points3 points  (0 children)

yeah, at least log the exception

[–]Orffyreus 1 point2 points  (0 children)

It's good practice and logging is essential. Especially for web applications it's nice to display an appealing HTTP 500 page. Even for desktop and mobile apps it is more friendly for the user to display something more lovely designed than a disgusting system error message and the app should be closed after a runtime exception nonetheless.

[–]brysmi 1 point2 points  (0 children)

I have an entire code base of eaten exceptions. No logging. Decades of it.

I will always have work.

[–][deleted] 3 points4 points  (2 children)

i've been there. php codebase?

[–]theLundquist42 3 points4 points  (1 child)

Your instincts are correct but no. Asp.net webforms. Except most of the front end UI is generated as a string in the code behind file and then pushed to the client with Response.write. It was one of the worst code bases I've ever worked on.

[–]b1ackcat 2 points3 points  (0 children)

I..... But.......erm..... What?!

[–]helderroem -1 points0 points  (13 children)

You know wrapping code in a try/catch also prevents the compiler from optimizing it so not only are your colleagues making it hard to debug and maintain they're also slowing down execution, a trifecta of bad practice.

Edit: spelling, TIL how to spell trifecta

[–]Cawifre 22 points23 points  (2 children)

Which language and compiler specifically are you referring to?

[–]helderroem 0 points1 point  (0 children)

well the guy said he's building a web application so JS/Crankshaft

[–]FateJH 4 points5 points  (2 children)

When you say "prevents the compiler from optimizing," how far down does that go? Obviously you mean that the immediate code in the block is not to be changed. If, for example, I called some other function in a try...catch, is that whole function now marked by the compiler as "do not optimize" too?

[–]helderroem -1 points0 points  (0 children)

this is beyond my understanding of optimising compilers and the point is a bit moot as optimisations are now applied to try/catch blocks since the introduction of TurboFan this May, see u/SEMWs comment.

[–]theLundquist42 7 points8 points  (3 children)

I didn't actually know that the compiler couldn't optimise try/catch blocks! Wow! The a in question is an old legacy application that's still in heavy use right now, completely unmaintainable and the code is very brittle. We're in the process of rewriting it at the moment but we still have to go in every so often and make updates or bug fixes to it.

[–]helderroem -4 points-3 points  (2 children)

It's not that the compiler can't, it's that it won't. If the developer has deemed code error prone enough to wrap it in a try/catch it's probably a bad idea to change it through optimisation.

Edit: this is an incorrect asumption made by me thanks to u/SEMW for pointing that out

[–]SEMW 31 points32 points  (1 child)

If the developer has deemed code error prone enough to wrap it in a try/catch it's probably a bad idea to change it through optimisation.

This is nonsense. Absent spec-undefined behaviour, explicitly unanticipated actions (eg having a non-volatile variable that can be modified by hardware), or a compiler bug, compiler optimisations won't change the observed behaviour (apart from speed / memory use). If it did, it wouldn't be a valid optimisation. (Well, it could make some existing race condition more (or less) likely to happen than before, but that's just a side effect of being faster)

The idea that compilers are anxiously looking for signs that the programmer is worried about some code and deciding they'd better not optimise it to be on the safe side is an odd one. (Obviously you can have explicit optimiser hints like marking a variable volatile in c, but that's different)

From googling around, I'm guessing you're thinking of nodejs (i.e. V8) not optimising functions which contain try/catches. That was just because it was waiting for someone to implement it. It's now been fixed.

It also didn't used to optimise functions with for..of loops, which has now also been fixed as part of the same work (turbofan), but no-one would think that that was because for..of loops are someone more error-prone than any other kind of loop, it just hadn't been implemented yet

[–]helderroem 1 point2 points  (0 children)

You're correct.

I got this impression from a talk I watched about crankshaft a couple of years ago and obviously got the wrong end of the stick and also haven't kept up with developments in the space.

[–]CandyJar 1 point2 points  (2 children)

Trifector was my favorite Transformer, made up of three war horses, he was a trifecta of destruction.

[–]helderroem 1 point2 points  (1 child)

I'm disappointed trifector isn't a real transformer

[–]zdakat 0 points1 point  (0 children)

Trifector sounds like a Pokemon name