处理 "除以零" 异常的示例
在此示例中:
- 可绑定 API CEEHDLR 在函数
main()中注册main_hdlr。 - 可绑定 API CEEHDLR 在函数
fred()中注册fred_hdlr。 - 发生 MCH1211 (除以零) 异常。
- 调用处理程序
fred_hdlr以测试异常是否为 MCH1211。 - 条件处理程序中的结果代码设置为渗入到下一个条件处理程序。
- 处理程序
fred_hdlr返回而不处理异常,导致调用main_hdlr。 - 用户提供的令牌将更新为值 "1" ,并设置结果代码以处理异常。
- 将返回处理程序
main_hdlr,并处理异常。 - 在导致除零的语句之后,控制将在
fred()中恢复。
此示例使用 T1520IC7 -ILE C Source to Percolate a Message to Handle a Condition中显示的源。
- 要创建程序 T1520IC7,请输入:
CRTBNDC PGM(MYLIB/T1520IC7) SRCFILE(QCPPLE/QACSRC) - 要运行程序 T1520IC7,请输入:
输出为:CALL PGM(MYLIB/T1520IC7)in fred_hdlr, percolate exception. in main hdlr: Facility_ID = MCH, MsgNo = 0x1211 Resume here because resume cursor not moved and main_hdlr handled the exception. A condition was percolated from fred() to main() and was then handled. Press ENTER to end terminal session.
T1520IC7 -ILE C 源以执行 "要处理条件的消息"
/* This program uses the ILE bindable API CEEHDLR to enable handlers */
/* that percolate and handle a condition. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <leawi.h>
/* A condition handler registered by a call to CEEHDLR in fred(). */
void fred_hdlr( _FEEDBACK *cond, _POINTER *token, _INT4 *rc, _FEEDBACK *new )
{
if (!memcmp(cond->Facility_ID, "MCH", 3) && cond->MsgNo == 0x1211)
{
*rc = CEE_HDLR_PERC; /* ... let it percolate to main ... */
printf("in fred_hdlr, percolate exception.\n");
}
else
{
*rc = CEE_HDLR_RESUME; /* ... otherwise handle it. */
printf("in fred_hdlr, handle exception.\n");
}
}
/* A condition handler registered by a call to CEEHDLR in main(). */
void main_hdlr( _FEEDBACK *cond, _POINTER *token, _INT4 *rc, _FEEDBACK *new )
{
printf("in main hdlr: Facility_ID = %.3s, MsgNo = 0x%4.4x\n",
cond->Facility_ID, cond->MsgNo);
**(_INT4 **)token = 1; /* Update the user's token. */
*rc = CEE_HDLR_RESUME; /* Handle the condition */
}
int fred(void)
{
_HDLR_ENTRY hdlr = fred_hdlr;
_FEEDBACK fc;
int x,y, zero = 0;
/* Register the handler without a token. */
CEEHDLR(&hdlr, NULL, &fc);
if (fc.MsgNo != CEE0000)
{
printf("Failed to register the condition handler\n");
exit(88);
}
/* Cause a divide by zero condition. */
x = y / zero;
printf("Resume here because resume cursor not moved and main_hdlr"
" handled the exception\n");
}
int main(void)
{
_HDLR_ENTRY hdlr = main_hdlr;
_FEEDBACK fc;
volatile _INT4 token=0, *tokenp = &token;
/* Register the handler with a token of type _INT4. */
CEEHDLR(&hdlr, (_POINTER *)&tokenp, &fc);
if (fc.MsgNo != CEE0000)
{
printf("Failed to register the condition handler\n");
exit(99);
}
fred();
/* See if the condition handler for main() updated the token. */
if (*tokenp == 1)
printf("A condition was percolated from fred() to main() and"
" was then handled.\n");
}