107
Feb 18 '21
#DEFINE yeet throw
34
Feb 18 '21
#define yeet throw new Exception ("yolo");14
u/sanderhekkermen Feb 18 '21
That exception is now floating somewhere in memory
1
Mar 05 '21
Good! I think I want to keep it on hand. How do I permanently keep something in my registers at all times? Never know when you'll need it as fast as possible!
14
87
u/FireBlazer2020 Feb 18 '21
Honestly if you left a darn good comment in there on why it's empty and why that's intended, I don't think that's too bad. Unless its a program breaking error and you just don't care enough to implement an actual fix. And also, if you're gonna do something like that, make sure to catch the specific exception that you are safely able to ignore instead of literally every exception
57
16
u/Kambz22 Feb 18 '21
I don't think its good ever. You really should at minimum log/output something out somewhere. Something is interacting or expecting results from what's inside that try statement. If there's an exception causing it to fail, you won't know if it's your GUI, your data, or whatever.
15
u/tbriz Feb 18 '21
I am astonished at the amount of upvotes on this comment. If you're a professional writing software for end users, please, fellow devs, do not do this. There is almost always a better way. Output the error somewhere. Investigate it. Fix it.
This is bad, lazy, and hacky and should be avoided at all costs.
I inherited a program where the dev before me used this "technique". As a result, the end users were unaware that they were saving bad data on a regular basis because the program never stopped to tell them it was catching errors. It caused many headaches for me and the end users cleaning up the mess that was made.
4
u/ArdiMaster Feb 18 '21
E.g. in Java, closing a file can throw. Like, what am I going to do with that error, besides maybe logging it somewhere? Especially if a preceding read or write has already failed.
(And IIRC that's a checked exception, so the compiler forces you to catch it at some point.)
5
u/tbriz Feb 18 '21
My gut would tell me closing a file shouldn't throw unless something went wrong - and I would want to investigate it, learn why it's happening, and fix the code in order to feel comfortable that this would not lead to incomplete file saves, corrupted output, etc., when the user gets their hands on it.
However, I have not experienced this situation first hand with Java so I could not speak specifically to it. There is almost always a better way - but in the rare case sometimes we have to do (what I would refer to as) hacky stuff.
6
u/1842 Feb 19 '21
Java has exceptions that basically have to be dealt with if they're defined to be of a certain type - they're called "checked exceptions". It's one of my least favorite things about Java.
I just wrote a small library for internal business use. It does some network, json parsing, session stuff. I basically converted all the checked exceptions (and there were tons of them) into custom unchecked ones for consumption by the implementing app. I remember having one checked exception that I had to catch, but didn't care if it threw or not. I can't remember exactly the reason... I'll update when I find it tomorrow.
Things like that come up rarely. Good convention is to
} catch (Exception ignored) {}5
Feb 18 '21
The try-catch is mandatory when writing data in a file, otherwise the program does not run, You would make sure the exception doesnt happen but the try-catch needs to be there, so basically only logging it somewhere is legal and a very common practice.
1
u/1842 Feb 19 '21
Following up, it looks like the code I wrote that ignored an exception is part of some session cleanup during logout. It's removing some data, but if it fails for some reason, that data must already be missing. Not much reason to bother with it outside of debug reasons.
24
u/unnecessary_Fullstop Feb 18 '21
How do you make these pics? Are there some generator that I can simply copy paste code into and get this?
.
46
u/BleedObsidian Feb 18 '21
Yeah it’s a website called Carbon: https://carbon.now.sh
17
Feb 18 '21
now lets all go make the web a little less accessible one piece of shit code at a time
10
u/TCOO1 Feb 18 '21
You can iframe it straight in a webpage. That way someone else can still copy it and screen readers work.
When changelog had an interview with the carbon creator, he said that he himself disapproves of the use of carbon generated images in blogs/tutorials/etc, citing the accessibility. Rather recommending using the (built in) iframe functionality.But for example for twitter this should be a perfectly fine solution.
3
u/unnecessary_Fullstop Feb 18 '21
Thank you so much.
.
8
17
u/MicrosoftFuckedUp Feb 18 '21
I've seen worse - I've seen applications swallowing up Throwables... "Oh so you got an OutOfMemoryError? Lemme just keep on running on this absolutely undefined heap state, see what happens"
5
u/Elsolar Feb 18 '21
IIRC
Exceptionin C# is equivalent toThrowablein Java, in the sense that it includes errors like out of memory and stack overflow. So it's pretty bad!Stuff like this is actually why I think Java's exception model is better than that of C# or C++. The separation of
ErrorandExceptioninto separate catagories makes it easy to specify that you want to catch any error except for really catastrophic ones that bring down the whole process.2
u/Dealiner Feb 18 '21
It's not exactly like that in C#. You can catch
OutOfMemoryExceptionbecause honestly why not?StackOverflowExceptioncan be caught only in some specific instances, mainly when it's specifically allowed in CLR or when it's thrown directly in code. Besides that, there is alsoThreadAbortExceptionwhich is raised again after the catch block. There are also others that cannot be caught and some that could be caught in the older version of .NET Framework, but now they need special treatment, likeAccessViolationException, because they mean that the process state is corrupted. But I agree that this is less clear than in Java.0
u/Elsolar Feb 18 '21
I'd argue that catching out-of-memory exceptions is pretty bad practice overall. Generally speaking, there is almost nothing that a process can do to recover from (or even report) an out-of-memory condition. This is even more true for managed languages like Java or C#, where the programmer doesn't have any direct control over memory management. At least in C/C++, you might be able to save the process by freeing up some memory and retrying the allocation, but even then the vast majority of C/C++ programs are not robust in the face of allocation failures, even when they think they are. In general, anything you do to attempt to recover from an allocation failure is unlikely to do more than mangle your stack trace and obscure the failure's root cause.
On the other hand, I'm glad to hear that some of those other exception types are guarded by the runtime, especially since I've seen
catch(Exception e) { ... }in real production C# code at previous jobs. Exception handling in Windows has been pretty messy for a long time. It's good to see them clean it up a bit.1
u/Dealiner Feb 18 '21
I admit I'm not an expert, but according to this there are some reasons why catching
OutOfMemoryExceptioncan be helpful. It is also possible to try to force GC to collect withGC.Collect(), but it's hard to say how reliable it is, so that's probably not such a good idea.2
u/Elsolar Feb 19 '21
I'd be surprised if any modern GC implementations didn't attempt to collect garbage before throwing an out-of-memory exception. It doesn't really make sense to signal that the heap is full when it's mostly full of old objects. That's the way I was taught to implement garbage collection in school, anyway.
It's definitely true that some allocation failures can be recoverable. For example, just because you fail allocate a 4GB chunk doesn't mean that memory is exhausted. Maybe you can get away with a 2GB chunk and that's a reasonable fallback for your program. But if you ask the system for 8 bytes and that fails, then your process is hosed, and any attempt to recover will just cause further corruption and make debugging harder. The crux of the issue (in my opinion) is that
OutOfMemoryExceptionis used for both of these scenarios, even though one of them is a pretty mild error and the other is process-ending.
catch(Exception)is mostly useful in scenarios where reliability is distinctly preferable to failing fast. For example, if you're maintaining an IDE written in C# and I'm a plugin developer, you don't necessarily want to crash the user's whole editing experience just because my plugin contains a bug. In this case, it's reasonable for you to wrap invocations of my plugin code in try/catch blocks that prevent exceptions generated by the plugin from escaping into the surrounding program. In this situation, it's problematic thatOutOfMemoryExceptionis grouped together with other, less severe exceptions like index-out-of-range, null-pointer, and dictionary hash-ref errors that don't corrupt the entire process state. As I indicated in my original post, part of the reason I like Java's exception taxonomy is that it does a pretty good job of separating errors that indicate a problem with the program (or the data being processed by the program) and errors that indicate a problem with the process. The former category of errors are, as a rule, recoverable, so long as you don't care about whatever that code was trying to compute. The latter are, as a rule, not recoverable, and 99.9% of the time, you shouldn't even try.
OutOfMemoryExceptionskirts the line between these two categories, since it can represent either scenario, but I would argue that 100% of the scenarios in which you'd want to usecatch(Exception)are scenarios in which you wouldn't want to treat anOutOfMemoryExceptionas recoverable, since you have no way of distinguishing between anOutOfMemoryExceptioncaused bynew int[10000](maybe recoverable) and one caused bynew int(definitely not recoverable). If you have some place in your program where you're callingnew int[10000](or something similar), then you should just put a try/catch block around that particular allocation to check for failure, and use normal control flow to recover. Making the out-of-memory exception in C# a child ofExceptiondoesn't serve much purpose except to complicate error handling, imo.0
9
5
5
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Feb 18 '21
Image Transcription: Code
}
catch(Exception e)
{
//YOLO
}I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
4
2
u/virtulis Feb 18 '21
There are valid reasons to not give a damn about errors for specific piece of code.
4
u/hackintosh5 Feb 18 '21
Not any error. And not without logging it.
4
u/Kambz22 Feb 18 '21
Yeah there's too many people here who are fine with not at least logging the exception message in 1 line. I understand maybe if it's just some small personal project, but never in a professional one.
1
u/Pastill Feb 18 '21
I should send this to my old boss who called me out for doing this last time I worked at this corp. He has not rehired me for double the salary and I'll be starting in April.
1
1
u/Perdouille Feb 18 '21
It's not too bad if you explain why you don't need to handle the exception in a comment. I've had to do it too
1
1
1
1
u/TimelessTrance Feb 18 '21
I sadly have regularly used catch (Exception as e) in Python tons. It's either that or figure out which of the hundreds of exceptions to catch from Selenium. Usually just refresh the page and try again and it somehow works.
1
1
u/Fischchen Jul 06 '21
You've got to add _=e or else the compiler will Complain about an unused variable.
0
287
u/[deleted] Feb 18 '21
lol it’s such a nice picture then it’s just this piece of complete garbage inside it