- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
The frist thing that makes me uneasy about this code is the implication that it is
Global.Insert(string,string)
. "Global" ??? and what is it inserting? (Yeah, I know, two strings, but why would you insert them in something global?)Addendum 2024-04-03 06:58: Note that I read Remy's "Easy Reader Version" after posting the above...
Admin
Admin
Looks like a case of paid by the line.
Admin
Allows for easy setting of breakpoints on specific exception types without modification or knowing how to use conditional breakpoints.
Admin
No. No. No. This is just a stub (to be copied and pasted everywhere in the code) to handle any type of exception that might occur. If you really need the OutOfMemory exception, you already have the template to 'Insert' the appropriate string (preferably hard-coded.)
Admin
Maybe an
OutOfMemoryException
should be thrown every time a dev forgets common sense.Admin
What are the chances that, if you have just thrown an
OutOfMemory
exception, inserting something into something else will trigger anotherOutOfMemory
exception?Admin
Better than average, I'd say. (OK, yes, it's possible to avoid that problem by preallocating the container used by
Global.Insert
to have empty elements for its maximum capacity, but do you think the sort of programmers who'd write something like this would think of such a thing?)Admin
It depends. There are 2 typical causes for the exception: https://learn.microsoft.com/en-us/dotnet/api/system.outofmemoryexception?view=net-8.0
If you're just doing bad stuffs with a StringBuilder object, then maybe the insertion won't cause another OutOfMemoryException.
If the computer does run out of memory, then it's unclear. The documentation says that you should handle it like something unrecoverable. But on the other hand, it also says that the system ran out of contiguous virtual address space just for a particular operation, so it's not clear if there could still be enough contiguous virtual address space for stuffs like the insertion.
Admin
The least bad case is, Global is short for GlobalLogging or something like that...
Admin
At first glance I thought that's the usual Java checked exception hell. But then I realized that there's an NullReferenceException instead of a NullPointerException, and now I'm pretty sure it was a Java developer importing the checked exception hell.
Admin
Ironically, slim.
Memory structures in .net are generally based on preallocated arrays and if you have dynamic growth, then a second array with additional headroom will replace the first one after the content is copied.
In other words, if you don't specifically restrict the capacity of dynamic collections to a specific exact value at initialization or exactly fill the collection up to the current capacity, there's a big chance that you still have room left for multiple additional additions.
Plus, keep in mind, an out of memory failure is often just a temporary failure. Modern OS have plenty of room to recover from temporary memory shortages; so an out of memory issue can turn to a non-issue a few microseconds later. It's actually super hard to ran out of memory permanently these days if you are not really, really forcing the issue.
Admin
I don't find "Global" here evil--that's no doubt a systemwide logging system. When they map to real world resources (say, a log file) you end up with something approximating a global no matter what you do.
Admin
"Well, I guess that if they ever need to add different code paths, they're halfway there."
I once worked with a fairly smart developer who unironically thought this was a good idea. Not specifically for exceptions but the idea of duplicating many lines just in case you ever need to change one to be different.
Admin
"Modern OS have plenty of room to recover from temporary memory shortages; so an out of memory issue can turn to a non-issue a few microseconds later. It's actually super hard to ran out of memory permanently these days if you are not really, really forcing the issue." At least in java this is not the case. When you get an OOM, it means the JVM has tried really hard to obtain that extra memory needed, and failed miserably, and there is nothing you could reasonably do to prevent everything from crashing and burning. It is actually an antipattern to try and catch that particular exception (and you might expect a broken wrist if you still attempt to do it).
Admin
I’m remembering back from the dark ages that Java 1.4 (or maybe it was 1.2) didn’t have any way to catch multiple exceptions in the same block. These days, you can separate the exception types with a pipe symbol, but I don’t think that was available originally.
Therefore, in that version of Java, this is the only way to catch a specific set of typed exceptions that might be thrown. Sure, you could use a generic Exception - but then you’ll be catching other exception types that you might not have wanted to handle this way. If there’s no super class common to the exception types that you want to catch, this is your only solution.
So, I guess TRWTF is old versions of Java, that have since been changed, and old, perfectly working code, that hasn’t been updated to the newest standard yet…
Admin
Am I the only one to think that the original developer just didn't know how to catch all exceptions?
Admin
wrt Nick's question, are any of those caught exceptions NOT subclasses of Exception (caught by the final stanza)?
Admin
Keep in mind, this is .net code, not Java code. Those shortcomings of Java don't apply with a natively compiled language like C#, because there was never a VM (or whatever you want to call the Java interpreter).
In .net it is also an anti-pattern to catch an exception if you can't handle it. The reason is pretty simple, because C# is a natively compiled language the compiler generates assembly code using native exception handling APIs. And those are all but free, even if you don't throw an exception. So generally you should handle exceptions as soon as possible or just let them escalate up the call stack until someone can handle them. On the very top you can finally have a global exception scope, which gets the exception and logs it before failing; cause if you don't handle an exception in the first place, there's no need to log it - after all exception have stack traces in .net; you know exactly where it happened and packing an exception into an other exception while doing nothing else, is also considered an anti-pattern.
Admin
There used to be a third main reason back when .NET still supported GDI+: Opening a corrupt image would throw OutOfMemoryException.