21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

tion gets caught. The only ways to avoid running such a block are to do one of<br />

the following:<br />

• Return from the current function.<br />

• Use goto to jump to a label outside the exception-handling block.<br />

• Break the abstraction that the XXL macros provide.<br />

All of these techniques are bad form. Circumventing the XXLexception structure<br />

will cause its exception handler stack to enter an inconsistent state, resulting<br />

in unexpected and often catastrophic behavior. You should never return<br />

from within a TRY, CATCH, EXCEPT, orFINALLY block, nor should you ever use any<br />

other method, such as goto or longjmp, to jump between blocks or outside of<br />

them.<br />

• XXLrequires you to use END_TRY. This is necessary because of the way XXLis<br />

implemented as preprocess macros; true exception handling requires handling at<br />

the language level, which is a luxury that we do not have with C.<br />

• The syntax for actually raising an exception differs. XXLhas a THROW() macro<br />

that takes two parameters. The first is the exception code, and the second is a<br />

void *, representing arbitrary data that you might want to pass from the site of<br />

the raise to the exception handler. It is acceptable to pass in a NULL value as the<br />

second parameter if you have no need for it.<br />

If you want to get the extra information (the void *) passed to the THROW( )<br />

macro from within an exception handler (specifically, a CATCH(), EXCEPT, or<br />

FINALLY block), you can do so by calling EXCEPTION_INFO( ).<br />

• In some cases, the XXLmacro set may conflict with symbols in your code. If that<br />

is the case, each also works if you prepend XXL_ to the macro. In addition, you<br />

can turn off the basic macros by defining XXL_ENFORCE_PREFIX when compiling.<br />

Once you have an exception-handling mechanism in place, we recommend that you<br />

avoid calling functions that can return an error when they fail.<br />

For example, consider the malloc( ) function, which can return NULL and set errno to<br />

ENOMEM when it fails (which only happens when not enough memory is available to<br />

complete the request). If you think you will simply want to bail whenever the process<br />

is out of memory, you could use the following wrapper:<br />

#include <br />

void *my_malloc(size_t sz) {<br />

void *res = malloc(sz);<br />

if (!res) {<br />

/* We could, instead, call an out of memory handler. */<br />

fprintf(stderr, "Critical: out of memory! Aborting.\n");<br />

abort( );<br />

}<br />

return res;<br />

}<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Performing Error Handling | 703

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!