void main() { //初始化 ... try { ProcessMail(...); } catch (int ret) { switch (ret) { case E_INITIALIZATION_FAILURE: ... case E_IRRECOVERABLE: ... ... } } }
void ProcessMail(...) { //初始化 ... if ( initializationError ) { throw(E_INITIALIZATION_FAILURE); }
while ( !shutdown ) { try { ReadMail(...) } catch (int ret) { switch (ret) { case E_READ_ERROR: //记录错误信息 ... //试图恢复 ... if ( recovered ) { continue; } else { throw(E_IRRECOVERABLE); } break; case ... } } //继续处理 ... }
//throw()可以用来取代缺少的返回码 //但也要注意由此带来的性能损失
throw(S_OK); } // ProcessMail()
void ReadMail(...) { ... //在此无须捕捉异常 nBytesAvailable = ReadBytes(...) ... }
int ReadBytes(...) { //读取数据 if ( error ) { throw(E_READ_ERROR); } return nBytesRead; } |