Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Who needs exceptions?

Name: Anonymous 2012-07-04 20:46


//in some class or namespace:
typedef unsigned int error_code;
const error_code out_of_mem = 0x01,
  niggers = 0x02,
  jews = 0x04;

error_code f()
{
  error_code ret = 0;
  //do some stuff...
  //suddenly niggers
  ret |= niggers;
  //but it's not a fatal error so we will continue execution
  //...
  if(some_pointer == nullptr)
  {
    ret |= out_of_mem;
    goto cleanup;
  }
  //and so on...
cleanup:
  delete some_pointer;
  //etc...

  return ret;
}

void implementation()
{
  error_code f = f();
  if( f & out_of_mem )
    //handle error
    printf("%s\n", "allocation failure");

  if( f & niggers )
    pool.close();
}


The main point of exceptions is to separate error detection from error handling. This accomplishes that. If you need the advanced features of exceptions, take a good look at your design and, more importantly, your life.

Name: Anonymous 2012-07-04 21:34

>>3
Yeah, but your program now has run into a serious problem: it's run out of available memory, but it may not stop trying to do things that it would normally do.  Continuing as things are could create problematic states elsewhere, jeopardizing stability or configuration, and you wouldn't notice until the damage became significant or unless you checked an error log.

Sure, you can just quit the program here if the problem detected is that serious; I'm not saying that.  But that still means having to put redundant code that handles the affairs of reporting back to the user/developer what went wrong and where.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List