Chinaunix首页 | 论坛 | 博客
  • 博客访问: 194573
  • 博文数量: 7
  • 博客积分: 1712
  • 博客等级: 上尉
  • 技术积分: 371
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-28 14:54
文章分类
文章存档

2012年(4)

2010年(3)

分类: 系统运维

2012-03-19 11:38:07

我在2006年以前的博文中(http://blog.chinaunix.net/uid-583146-id-2090007.html) 提到了如何结合使用INFDS关键词定义的文件反馈信息结构和INFSR关键词定义的*PSSR意外错误控制程序.


i5/OS 5.1版本之后, ILE RPG显然对如何进行错误处理进行了很多增强。有在操作符上使用E-extender, 以及使用%Error%StatusBIFs, 以及使用monitor groups的。这些错误通常是在程序员知道在什么地方会发生错误的情况下而进行处理。本篇文章不讨论这些。


经常会有很多错误是出乎程序员预料的,程序员也无法预见什么时候什么地方发生错误,但是由希望RPG程序能够自动捕捉到异常错误,并转到错误处理例程进行处理或恢复,而不是程序在运行过程中因发生异常错误而终止运行。本文使用一个具体例子展示ILE RPG如何进行异常条件处理(Condition Handlers)。

 

异常条件处理关键是这几步:

  • PRG4 新提供了一个CEE类型的API: CEEHDLR, 它可以在运行时登记你程序中要处理异常错误的过程名,这称为 ILE Condition Handler,这样发生异常错误的时候, ILE就能把异常错误转到这个API登记的用户自定义的错误处理过程(ILE Condition Handler)
  • CEEHDLR使用过程指针来指向登记的Condition Handler。过程指针指向过程的地址。参数入口如下,第一个参数就是过程指针。

0011.00 D CEEHDLR         PR                                

0012.00 D  pConHdlr                       *   PROCPTR       

0013.00 D  CommArea                       *   Const         

0014.00 D  Feedback                     12A   OPTIONS(*OMIT)

  • 登记过程:先定义一个过程指针变量,取自定义错误处理例程地址:

0001.00 D pConHdlr        S               *   PROCPTR                 

0002.00 D                                     INZ(%paddr('CONDHDLR')) 

 

  • 在ILE RPG应用程序中调用CEEHDLR 登记异常错误处理过程,通常在程序运行的开头即调用该API。

0020.00   CEEHDLR(pConHdlr:%ADDR(Error):*OMIT);

 

以下是 1000来获得一个异常错误的RPG程序:

 

0001.00 D pConHdlr        S               *   PROCPTR                          

0002.00 D                                     INZ(%paddr('CONDHDLR'))          

0003.00 D Error           S              1N   inz(*off)                        

0004.00 D Arr1            S             10A   Dim(5)                           

0005.00 D Message         S             40A   INZ                              

0006.00                                                                        

0007.00 D Hundred         S              5  0 INZ(100)                         

0008.00 D Zero            S              5  0 INZ(0)                           

0009.00 D Examp           S              5  0 INZ                              

0010.00                                                                         

0011.00 D CEEHDLR         PR                                                   

0012.00 D  pConHdlr                       *   PROCPTR                          

0013.00 D  CommArea                       *   Const                             

0014.00 D  Feedback                     12A   OPTIONS(*OMIT)                   

0015.00                                                                                                                                        

0016.00  /Free                                                         

0017.00   Message = 'Starting CauseErr';                               

0018.00   Dsply Message '*REQUESTER';                                  

0019.00                                                                 

0020.00   CEEHDLR(pConHdlr:%ADDR(Error):*OMIT);                        

0021.00   Message = 'In CauseErr - causing Division error';            

0022.00   Dsply Message '*REQUESTER';                                  

0023.00                                                                

0024.00   Examp = %Div(Hundred:Zero);                                  

0025.00   Message = 'Resume CauseErr';                                 

0026.00   Dsply Message '*REQUESTER';                                   

0027.00                                                                 

0028.00   Exsr ErrHdl;                                                  

0029.00   *InLR = *On;                                                  

0030.00   Return;                                                       

0031.00                                                                 

0032.00   BegSr ErrHdl;                                                  

0033.00     If Error;                                                   

0034.00        Message = 'In CauseErr Subr - Error detected';           

0035.00        Dsply Message '*REQUESTER';                              

0036.00        Error = *Off;                                           

0037.00     Endif;                                                     

0038.00   EndSr;                                                       

0039.00  /End-free                                                      

 

 

再写一个异常条件处理过程(Condition Handler),

0000.01 H NoMain                                                               

0001.00                                                                        

0002.00 D CondHdlr        PR                                                    

0003.00 D  Parm1                        12A                                    

0004.00 D  Parm2                          *                                    

0005.00 D  Parm3                        10I 0                                   

0007.00                                                                        

0008.00 PCondHdlr         B                   Export                           

0008.01 D CondHdlr        PI                                                   

0009.00 D  InToken                            Like(CondToken)                  

0010.00 D  pErrInfo                       *                                    

0011.00 D  Action                       10I 0                                  

0012.00                                                                        

0013.00 DCondToken        DS                  Based(IntokPoint)                

0014.00 D  CondSev                       5I 0                                  

0015.00 D  CondMsgNo                     2A                                    

0016.00 D                                1A                        

0017.00 D  CondPrefix                    3A                        

0018.00 D                                4A                         

0018.01                                                            

0018.02 DError            S              1N   Inz                  

0018.03 DMessage          S             40A   INZ                  

0018.04 DResume           S             10I 0 Inz(10)              

0019.00                                                            

0020.00  /Free                                                     

0021.00   Message = 'Starting CondHdlr';                        

0022.00   Dsply Message '*REQUESTER';                           

0023.00   Error = *On;                                          

0024.00   Action = Resume;                                      

0025.00   Return;                                               

0026.00  /End-free                                              

0027.00                                                         

0028.00 P                 E                                     

 

 

编译运行程序,输出结果,可以看到当RPG程序运行到1000后,发生异常错误,程序自动捕捉到错误,并转到异常处理过程里,并恢复了程序继续执行:

DSPLY  Starting CauseErr                              

DSPLY  In CauseErr - causing Division error           

DSPLY  Starting CondHdlr                              

DSPLY  Resume CauseErr                                

阅读(3215) | 评论(1) | 转发(4) |
给主人留下些什么吧!~~

happenZheng2013-07-28 16:09:11

顺便问下,错误处理过程(ILE Condition Handler)的参数是不是固定格式的?
错误处理过程里,需要对错误信息标识为已处理?