gue*_*tli 2 abap exception try-catch
我想抓住并处理SAPSQL_DATA_LOSS我的ABAP代码。
我尝试了这个:
try.
SELECT *
FROM (rtab_name) AS rtab
WHERE (sub_condition)
into table @<sub_result>
.
catch SAPSQL_DATA_LOSS into error.
...
endtry.
Run Code Online (Sandbox Code Playgroud)
但以上代码无效。我收到此消息:
类型“ SAPSQL_DATA_LOSS”无效
我尝试了这个:
catch SYSTEM-EXCEPTIONS SAPSQL_DATA_LOSS = 123.
SELECT *
...
.
endcatch.
if sy-subrc = 123.
...
endif.
Run Code Online (Sandbox Code Playgroud)
但是上面的代码给了我:
而不是“ SAPSQL_DATA_LOSS”预期的“系统例外”(由我从德语翻译为英语)
怎么抓SAPSQL_DATA_LOSS?
这个问题不是关于“为什么会发生这种异常?”。这已经解决了。我的代码应处理该异常。
SAPSQL_DATA_LOSS 是运行时错误。
由于SAPSQL_DATA_LOSS不是基于类的异常,因此无法使用捕获它try catch。
由于SAPSQL_DATA_LOSS不是可捕获的运行时错误,因此无法使用捕获它try catch SYSTEM-EXCEPTIONS。
请参阅以下可捕获的运行时错误。
https://help.sap.com/doc/abapdocu_751_index_htm/7.51/zh-CN/abenueb-abfb-sysexc.htm
After some tries I can propose you a possible solution.
This is a workaround:
I don't know if it can be applied to your case, since it needs the select statement to be wrapped into an RFC function module !
The main point is that a short dump (message type X) CAN be handled in RFC calls.
So using an RFC (CALL FUNCTION 'xxxxx' destination 'NONE' for example) and using special exception SYSTEM_FAILURE, the system does not terminate the caller program, but instead it returns a SY-SUBRC > 0 with the Short dump informations in system message fields (SY-MSGxx).
STEPS
Create a Function module (RFC enabled) with your select statement input + the row type of the result table. (All parameters passed by value)
You need this last parameter since generic tables can't be passed in RFC (no "TYPE ANY TABLE" allowed)
FUNCTION Z_DYN_SEL .
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" VALUE(RTAB_NAME) TYPE TABNAME16
*" VALUE(SUB_CONDITION) TYPE STRING
*" VALUE(RESULT_TYPE) TYPE STRING
*"----------------------------------------------------------------------
* RTAB_NAME: DB Table
* SUB_CONDITION: WHERE Condition
* RESULT_TYPE: The ROW type of the internal table
field-symbols <sub_result> type any table.
* DEFINE LOCAL DYNAMIC TABLE TO STORE THE RESULT
data: lr_res type ref to data.
create data lr_res type standard table of (result_type).
assign lr_res->* to <sub_result>.
* DYNAMIC SELECT
select *
from (rtab_name) as rtab
where (sub_condition)
into table @<sub_result>.
* EXPORT RESULT TO A MEMORY ID, SO IT CAN BE RETRIEVED BY CALLER
export res = <sub_result> to memory id 'RES'.
Run Code Online (Sandbox Code Playgroud)
Main program: In this caller example some parameters are passed to the RFC.
KTOKD field (should be 4 chars long) is passed with a char10 value (producing your short dump).
If ANY Dump is triggered inside the function, we can now handle it.
If everything went fine, IMPORT result from the EXPORT statement inside the RFC
field-symbols <sub_result> type any table.
data: lr_res type ref to data.
create data lr_res type standard table of KNA1.
assign lr_res->* to <sub_result>.
data lv_msg type char255.
call function 'Z_DYN_SEL' destination 'NONE'
exporting
rtab_name = 'KNA1'
sub_condition = `KTOKD = 'D001xxxxxx'`
result_type = 'KNA1'
exceptions
system_failure = 1 message lv_msg.
if sy-subrc = 0.
import res = <sub_result> from memory id 'RES'.
else.
write: / lv_msg.
write : / sy-msgid, sy-msgno, sy-msgty, sy-msgv1, sy-msgv2, sy-msgv3, sy-msgv4.
endif.
Run Code Online (Sandbox Code Playgroud)
RESULTS
After the RFC call in case of a short dump in the select statement, the program is not terminated and the following pieces of information are available
SY-SUBRC = 1
Run Code Online (Sandbox Code Playgroud)
lv_msg is the error text (Data was lost while copying a value.)
Sy-msgid = 00
Sy-msgno = '341'
Sy-msgty = 'X'
Sy-msgv1 = 'SAPSQL_DATA_LOSS'
Run Code Online (Sandbox Code Playgroud)