Code: Select all
try
{
DoLotsOfImportantStuff();
DoMoreImportantStuff();
Etc();
}
catch (OutOfMemoryException ex)
{
// EMPTY
}
"Meh. Don't worry about."
Moderator: Coders of Rage
Code: Select all
try
{
DoLotsOfImportantStuff();
DoMoreImportantStuff();
Etc();
}
catch (OutOfMemoryException ex)
{
// EMPTY
}
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
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?GyroVorbis wrote:If I had exceptions enabled, that would be how I'd handle running out of RAM on the Dreamcast in ES...
Yeah, that's essentially it. Load() anything, or anything that can epic fail returns a bool. It's as simple asKrolgar wrote:This sounds like something you'd see on thedailywtf.com.
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?GyroVorbis wrote:If I had exceptions enabled, that would be how I'd handle running out of RAM on the Dreamcast in ES...
Code: Select all
if(!level.Load("directory")) _dbgLog("Something is boned");
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.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.
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.mattheweston wrote:I think I get what you are saying but, it would be nice to see a snippet of code as an example.
Code: Select all
try {
Module.doStuff();
} catch(OutOfMemoryException ex) {
Module.dieInAFire();
}
Code: Select all
if(!level.Load("directory")) _dbgLog("Something is boned");
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.
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.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 ? }