Working on my first Pocket PC game, I found myself yearning for better error handling in my code base. Normally, I'd just use standard C++ exceptions, but eVC++ 3 doesn't support them.
Once I learned how setjmp/longjmp worked, I ended up rolling my own very simple exception-like framework.
I wanted my code to look like this:
I implemented this mechanism using macros. I'll get to the macro expansion in a bit, but first I'll explain the few globals I use.
I ended up using a simple class with enums for the exception codes, and a few static members. (It needn't be a class, for use in C.)
Now, the try/catch macros merely use setjmp in switch statements to organize the code:
The throw/rethrow macros use longjmp to do the jumping:
How does it work?
Basically, each CATCH becomes a case in a statement switching on the return code of setjmp. Case 0 is the TRY code; other cases use the exception code, and are the result of a longjmp. The default case does the CATCHALL. If there's no catchall, it automatically does a rethrow.
When you enter a TRY block, a jump environment is put on the stack. When you THROW, you longjmp to that jump environment to the case for the exception code (or the default case), and the jump environment is removed from the stack.
Auto objects are not destroyed as they are in real C++ exception handling. Also, I wouldn't do something like return from within a TRY block; it will mess up the jump environment stack.
This could be done in C easily. As it is, you only need to include <csetjmp> and define EXCEPTION_MAX for it to work. (I had troubles putting jmp_buf into other containers, so I just used an array.)
If you want messages, you could put them in the Exception class. You could also tie them into the THROW macro.
The beauty of my mechanism is that it is quite simple, once you understand how setjmp/longjmp work, and figure out the macros. I looked at some other exception implementations after making mine, and they all seemed too complicated.
Once I learned how setjmp/longjmp worked, I ended up rolling my own very simple exception-like framework.
I wanted my code to look like this:
- Code:
TRY
{
TRY
{
THROW(Exception::CodeB);
}
CATCH (Exception::CodeB)
{
RETHROW;
}
END_TRY_CATCH
}
CATCH (Exception::CodeA)
{
}
CATCHALL
{
}
END_TRY_CATCHALL
I implemented this mechanism using macros. I'll get to the macro expansion in a bit, but first I'll explain the few globals I use.
I ended up using a simple class with enums for the exception codes, and a few static members. (It needn't be a class, for use in C.)
- Code:
class Exception
{
public:
enum
{
CodeA = 1,
CodeB
};
static
jmp_buf m_aEnv[EXCEPTION_MAX];
static
int m_nDepth;
static
int m_nCode;
};
Now, the try/catch macros merely use setjmp in switch statements to organize the code:
- Code:
#define TRY \
{ \
++Exception::m_nDepth; \
switch (setjmp(Exception::m_aEnv[Exception::m_nDepth])) \
{ \
case 0: \
{
#define CATCH(nCode) \
break; \
} \
case nCode: \
{
#define CATCHALL \
break; \
} \
default: \
{ \
#define END_TRY_CATCHALL \
break; \
} \
} \
--Exception::m_nDepth; \
}
#define END_TRY_CATCH \
CATCHALL \
{ \
RETHROW; \
} \
END_TRY_CATCHALL
The throw/rethrow macros use longjmp to do the jumping:
- Code:
#define THROW(nCode) \
longjmp(Exception::m_aEnv[Exception::m_nDepth], Exception.m_nCode = nCode)
#define RETHROW \
longjmp(Exception::m_aEnv[--Exception::m_nDepth], Exception.m_nCode)
How does it work?
Basically, each CATCH becomes a case in a statement switching on the return code of setjmp. Case 0 is the TRY code; other cases use the exception code, and are the result of a longjmp. The default case does the CATCHALL. If there's no catchall, it automatically does a rethrow.
When you enter a TRY block, a jump environment is put on the stack. When you THROW, you longjmp to that jump environment to the case for the exception code (or the default case), and the jump environment is removed from the stack.
Auto objects are not destroyed as they are in real C++ exception handling. Also, I wouldn't do something like return from within a TRY block; it will mess up the jump environment stack.
This could be done in C easily. As it is, you only need to include <csetjmp> and define EXCEPTION_MAX for it to work. (I had troubles putting jmp_buf into other containers, so I just used an array.)
If you want messages, you could put them in the Exception class. You could also tie them into the THROW macro.
The beauty of my mechanism is that it is quite simple, once you understand how setjmp/longjmp work, and figure out the macros. I looked at some other exception implementations after making mine, and they all seemed too complicated.