背景:某天组内的同学发现有个B程序异常退出导致有些表没更新,最终发现是在A程序里sumbit B程序,然后B程序里有个指针没些CHECK <FS_TABLE> IS ASSIGNED导致报了GETWA_NOT_ASSIGNED。
心血来潮找了下相关资料。share给大家
1. Try catch
想在B程序里直接try catch cx_root。想法很简单,既然是异常,我直接catch你根结点总没问题了吧。不出意料打脸了,try catch cx_root里dump了。下面是2个程序,有兴趣的同学可以自己试一下。
*&---------------------------------------------------------------------*
*& Report YDEMO7
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT YDEMO7.
PERFORM FRM_SUBMIT.
*&---------------------------------------------------------------------*
*& Form FRM_SUBMIT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_submit .
FIELD-SYMBOLS:<FS_TABLE> TYPE ANY TABLE.
DATA:LV_EX_MSG TYPE STRING.
TRY.
LOOP AT <FS_TABLE> ASSIGNING FIELD-SYMBOL(<FS_TABLE_WA>).
ENDLOOP.
CATCH CX_ROOT INTO DATA(CX_ROOT_WA).
LV_EX_MSG = CX_ROOT_WA->get_longtext( ).
ENDTRY.
ENDFORM.
*&---------------------------------------------------------------------*
*& Report YDEMO8
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT YDEMO8.
PERFORM FRM_SUBMIT IN PROGRAM ('YDEMO7') IF FOUND.
2. CATCH SYSTEM-EXCEPTIONS
想试试CATCH SYSTEM-EXCEPTIONS的威力,好吧,和Try catch一个德兴。代码不做太多重复
3. 异步RFC
既然你不是想在B程序里更新表嘛,我把B程序单独拿出来做个异步RFC。这样主程序A不会受到影响,A程序里该跟新的表正常更新了。问题又来了,A程序是没问题了,但是B程序该dump还是dump了。好像问题只解决了一半。
最终找了下SAP标准的CX_ROOT类,发现好像确实没有指针的异常类。
总结:对于上述问题的,如果是指针类的该**CHECK <FS_TABLE> IS ASSIGNED.**还是得check,try catch 没招。但是对于其他的异常,try catch还是起作用的。比如下面这个非常常见的除零try catch就很好使了~
*&---------------------------------------------------------------------*
*& Report YDEMO7
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT YDEMO7.
PERFORM FRM_SUBMIT.
*&---------------------------------------------------------------------*
*& Form FRM_SUBMIT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_submit .
FIELD-SYMBOLS:<FS_TABLE> TYPE ANY TABLE.
DATA:LV_EX_MSG TYPE STRING.
DATA:LV_SREULT TYPE STRING.
TRY.
LV_SREULT = 1 / 0.
CATCH CX_ROOT INTO DATA(CX_ROOT_WA).
LV_EX_MSG = CX_ROOT_WA->get_longtext( ).
ENDTRY.
ENDFORM.
*&---------------------------------------------------------------------*
*& Report YDEMO8
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT YDEMO8.
PERFORM FRM_SUBMIT IN PROGRAM ('YDEMO7') IF FOUND.