Are you kidding me?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
TheBuzzSaw
Chaos Rift Junior
Chaos Rift Junior
Posts: 310
Joined: Wed Dec 02, 2009 3:55 pm
Current Project: Paroxysm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Are you kidding me?

Post by TheBuzzSaw »

Just found this today in our C# code base.

Code: Select all

try
{
    DoLotsOfImportantStuff();
    DoMoreImportantStuff();
    Etc();
}
catch (OutOfMemoryException ex)
{
    // EMPTY
}
"Hey! You're out of memory!"
"Meh. Don't worry about."

:nono:
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Are you kidding me?

Post by Falco Girgis »

:lol: :lol: :lol: :lol: :lol: :lol: :lol:
User avatar
k1net1k
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 563
Joined: Sun Nov 07, 2010 2:58 pm
Contact:

Re: Are you kidding me?

Post by k1net1k »

lol
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Are you kidding me?

Post by dandymcgee »

Rofl. :lol:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Are you kidding me?

Post by Falco Girgis »

If I had exceptions enabled, that would be how I'd handle running out of RAM on the Dreamcast in ES...
User avatar
Nokurn
Chaos Rift Regular
Chaos Rift Regular
Posts: 164
Joined: Mon Jan 31, 2011 12:08 pm
Favorite Gaming Platforms: PC, SNES, Dreamcast, PS2, N64
Programming Language of Choice: Proper C++
Location: Southern California
Contact:

Re: Are you kidding me?

Post by Nokurn »

This sounds like something you'd see on thedailywtf.com. :lol:
GyroVorbis wrote:If I had exceptions enabled, that would be how I'd handle running out of RAM on the Dreamcast in ES...
I've been wanting to move away from exceptions, and I should probably make a new topic to ask about this, but what strategy do you use for signaling errors in your classes? Do you only use the constructor for POD initialization and use a separate method for doing the heavy lifting?
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Are you kidding me?

Post by Falco Girgis »

Krolgar wrote:This sounds like something you'd see on thedailywtf.com. :lol:
GyroVorbis wrote:If I had exceptions enabled, that would be how I'd handle running out of RAM on the Dreamcast in ES...
I've been wanting to move away from exceptions, and I should probably make a new topic to ask about this, but what strategy do you use for signaling errors in your classes? Do you only use the constructor for POD initialization and use a separate method for doing the heavy lifting?
Yeah, that's essentially it. Load() anything, or anything that can epic fail returns a bool. It's as simple as

Code: Select all

if(!level.Load("directory")) _dbgLog("Something is boned");
User avatar
TheBuzzSaw
Chaos Rift Junior
Chaos Rift Junior
Posts: 310
Joined: Wed Dec 02, 2009 3:55 pm
Current Project: Paroxysm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Are you kidding me?

Post by TheBuzzSaw »

What kills me is that this particular application has been crashing left and right, and I found SEVERAL of this kind of try-catch block in the code. You think that if we were out of memory, we should report it, stop what we're doing, and exit. No, it just tries to keep running. It doesn't even ANNOUNCE that it's out of memory! It just goes until it implodes. Brilliant.
mattheweston
Chaos Rift Junior
Chaos Rift Junior
Posts: 200
Joined: Mon Feb 22, 2010 12:32 am
Current Project: Breakout clone, Unnamed 2D RPG
Favorite Gaming Platforms: PC, XBOX360
Programming Language of Choice: C#
Location: San Antonio,Texas
Contact:

Re: Are you kidding me?

Post by mattheweston »

Pardon my ignorance, but how would you gracefully handle an exception error with a simple if...else ? Wouldn't the exception cause the app to crash? The only alternative would be to identifiy every possible scenario for bad parameters and elimitate them.

I'm sure I'm missing something. Maybe Falco can give us some edjumacation. :)
Image
User avatar
TheBuzzSaw
Chaos Rift Junior
Chaos Rift Junior
Posts: 310
Joined: Wed Dec 02, 2009 3:55 pm
Current Project: Paroxysm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Are you kidding me?

Post by TheBuzzSaw »

mattheweston wrote:Pardon my ignorance, but how would you gracefully handle an exception error with a simple if...else ? Wouldn't the exception cause the app to crash? The only alternative would be to identifiy every possible scenario for bad parameters and elimitate them.
In exception-driven languages, you cannot stop every possible exception from occurring. Eventually, you have to make a decision on how to handle one when it is thrown. (In particular, it seems many database drivers insist on using exceptions as the only way of communicating.) However, you should always avoid making a try-catch part of the normal control flow. Exceptions are just that: exceptions. They should rarely happen. Check to see if your variable == null. Do not simply catch a NullPointerException to make your code clean looking. Log your exceptions as best you can. Try to kill off the situations that throw them.

Ultimately, you should do some exception-catching. Just don't maim your code with it.
mattheweston
Chaos Rift Junior
Chaos Rift Junior
Posts: 200
Joined: Mon Feb 22, 2010 12:32 am
Current Project: Breakout clone, Unnamed 2D RPG
Favorite Gaming Platforms: PC, XBOX360
Programming Language of Choice: C#
Location: San Antonio,Texas
Contact:

Re: Are you kidding me?

Post by mattheweston »

I think I get what you are saying but, it would be nice to see a snippet of code as an example.
Image
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Are you kidding me?

Post by Ginto8 »

mattheweston wrote:I think I get what you are saying but, it would be nice to see a snippet of code as an example.
Take, for example, the OutOfMemoryException of the original post. If you program runs out of memory, you probably want do one of two things: a) make a mad dash to free up some unnecessary memory or b) die noisily, providing as much information about the crash as possible so it can be debugged. For the second, you probably don't need try-catch at all really, since exception reports tend to be pretty verbose anyway.

However, what if you're using that original program as simply one section of a larger program? That larger program might not want to crash when a module runs out of memory; perhaps it just wants to kill the module. Then you'd add a try-catch somewhat like this:

Code: Select all

try {
    Module.doStuff();
} catch(OutOfMemoryException ex) {
    Module.dieInAFire();
}
But I think you were originally asking about Falco's code, not Buzz's.

Code: Select all

if(!level.Load("directory")) _dbgLog("Something is boned");
In this case, nothing is throwing an exception. Allowing exceptions is a rather complicated and costly process, requiring a step-by-step stack unwinding and a runtime check for catch blocks, so Falco decided to disable exceptions. To allow error checking for vital initialization steps that can fail (eg. loading the level), he separated initialization into two parts: the constructor, which would do POD (plain-old-data) initialization, consisting of things that cannot fail, and the secondary initialization(s) such as loading the level. These secondary initializations handle failure-prone processes and return a value indicating success or failure (in this case it returns a true value on success). Since the error is indicated by return, there's no need for an exception, and no need for exception handling. Hence, a conditional is used for error checking.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
mattheweston
Chaos Rift Junior
Chaos Rift Junior
Posts: 200
Joined: Mon Feb 22, 2010 12:32 am
Current Project: Breakout clone, Unnamed 2D RPG
Favorite Gaming Platforms: PC, XBOX360
Programming Language of Choice: C#
Location: San Antonio,Texas
Contact:

Re: Are you kidding me?

Post by mattheweston »

But let's say you have a method to do something

Code: Select all

bool erroredMethod()
{
  bool isSuccessful = false;
  do something;
  do something;  //if this causes an out of memory error(read exception) then the rest of the code doesn't execute
  do something;
  isSuccessful = true;  // how can this be set then ?

}
Image
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Are you kidding me?

Post by short »

mattheweston wrote:But let's say you have a method to do something

Code: Select all

bool erroredMethod()
{
  bool isSuccessful = false;
  do something;
  do something;  //if this causes an out of memory error(read exception) then the rest of the code doesn't execute
  do something;
  isSuccessful = true;  // how can this be set then ?

}
malloc returns NULL when it can't allocate any more memory in C. Higher level languages also provide constructs for informing the programmer that dynamic memory was unable to be allocated (default new in c++ returns NULL as well in every implementation I have seen). If your getting a memory error, check the return of malloc / new for null. if you fail to allocate dynamic memory (new() / malloc()) then set isSuccessful to false and return.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Van-B
Chaos Rift Regular
Chaos Rift Regular
Posts: 125
Joined: Tue Aug 10, 2010 7:17 am
Current Project: iPhone puzzle game
Favorite Gaming Platforms: All - Except Amiga
Programming Language of Choice: DBPro, ObjC++
Location: Scotland

Re: Are you kidding me?

Post by Van-B »

Hehe, love it :D

Kinda like that VB full-retard-mode command...

On Error Resume Next

In other words, if you crash, just go onto the next command and pretend it never happened.
Health, ammo.... and bacon and eggs.
Post Reply