// Conditional Catch Test // I may fold this back into terminate.cfa and resume.cfa once setting // up the non-trivial exception is reasonable to do. #include VTABLE_DECLARATION(num_error)( int (*code)(num_error *this); ); struct num_error { VTABLE_FIELD(num_error); char * msg; int num; }; const char * num_error_msg(num_error * this) { if ( ! this->msg ) { static const char * base = "Num Error with code: X"; this->msg = (char *)malloc(22); for (int i = 0 ; (this->msg[i] = base[i]) ; ++i); } this->msg[21] = '0' + this->num; return this->msg; } void ?{}(num_error & this, int num) { VTABLE_INIT(this, num_error); this.msg = 0; this.num = num; } void ?{}(num_error & this, num_error & other) { this.virtual_table = other.virtual_table; this.msg = 0; this.num = other.num; } void ^?{}(num_error & this) { if( this.msg ) free( this.msg ); } int num_error_code( num_error * this ) { return this->num; } VTABLE_INSTANCE(num_error)( num_error_msg, num_error_code, ); void caught_num_error(int expect, num_error * actual) { printf("Caught num_error: expected=%d actual=%d.\n", expect, actual->num); } int main(int argc, char * argv[]) { num_error exc = 2; try { throw exc; } catch (num_error * error ; 3 == error->virtual_table->code( error )) { caught_num_error(3, error); } catch (num_error * error ; 2 == error->virtual_table->code( error )) { caught_num_error(2, error); } try { throwResume exc; } catchResume (num_error * error ; 3 == error->virtual_table->code( error )) { caught_num_error(3, error); } catchResume (num_error * error ; 2 == error->virtual_table->code( error )) { caught_num_error(2, error); } }