在SAP 7.52中,我创建了一个基于NetWeaver类的异常,该异常在报告/程序中的that子句中执行时工作得很好。但是,在SAPGUI中没有显示自定义消息时,不会使用try子句来处理异常。
我要寻找的是,当未定义that时,此时使用的导出消息将显示在"UNCAUGHT_EXCEPTION“中。我尝试重新定义get_text( )和get_longtext( )方法。但是ABAP运行时错误并没有向开发人员提供任何有关原因的有用信息(它存储在异常的"attr_message“属性中)。
当使用"try catch“时,可以毫无问题地检索消息,但是SAPGUI在"ABAP运行时错误”报告中为开发人员提供了正确的消息。
zcx_adk_exception.abap
"! Base exception for all ABAP Development Kit (ADK) exceptions
class zcx_adk_exception definition public create public inheriting from cx_dynamic_check.
public section.
"! Initializes the exception message
"! @parameter message | Message related to the reason of the exception
methods constructor
importing value(message) type zadk_str optional.
"! Returns the message associated to the exception
methods get_message
returning value(result) type zadk_str.
methods if_message~get_text redefinition.
methods if_message~get_longtext redefinition.
private section.
data attr_message type zadk_str value ''.
endclass.
class zcx_adk_exception implementation.
method constructor ##ADT_SUPPRESS_GENERATION.
super->constructor( ).
if message is not initial.
me->attr_message = message.
endif.
endmethod.
method get_message.
result = me->attr_message.
endmethod.
method if_message~get_text.
result = me->get_message( ).
endmethod.
method if_message~get_longtext.
result = me->get_message( ).
endmethod.
endclass.工作正常的:
try.
raise exception type zcx_adk_exception exporting message = 'Base_Exception_Error'.
catch zcx_adk_exception into data(ex).
write: / 'Example 1:', ex->get_message( ).
write: / 'Example 2:', ex->get_text( ).
write: / 'Example 2:', ex->get_longtext( ).
endtry.输出如下:

不起作用的:
" Not Catching the exception
raise exception type zcx_adk_exception exporting message = 'Base_Exception_Error'. 这将导致显示以下消息

发布于 2021-01-01 23:10:48
按照以前提出的使用消息的想法,我想出了下面的代码,允许使用消息引发异常。这允许异常在“尝试捕获”块中调用时显示正确的消息,并在SAPGUI生成的转储的“错误分析”部分显示有用的消息。
解决方案:
"! Program to test functionalities and utilities
REPORT zsandbox_tests.
" Exception Class
CLASS lcl_exception DEFINITION INHERITING FROM cx_dynamic_check.
PUBLIC SECTION.
INTERFACES if_t100_dyn_msg.
METHODS if_message~get_text REDEFINITION.
METHODS constructor
IMPORTING VALUE(message) TYPE string.
PRIVATE SECTION.
DATA attr_message TYPE string VALUE ''.
ENDCLASS.
CLASS lcl_exception IMPLEMENTATION.
METHOD if_message~get_text.
result = attr_message.
ENDMETHOD.
METHOD constructor.
super->constructor( ).
me->attr_message = message.
ENDMETHOD.
ENDCLASS.
" Class that raises the exception
CLASS lcl_main DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main RAISING lcl_exception.
ENDCLASS.
CLASS lcl_main IMPLEMENTATION.
METHOD main.
DATA raise_message TYPE string VALUE 'Custom Message for the Exception'.
RAISE EXCEPTION TYPE lcl_exception
MESSAGE e000(lcl_exception) WITH raise_message '' '' '' " The if_t100_dyn_msg supports 4 attributes: V1, V2, V3 and V4 but I only use the first one
EXPORTING message = raise_message.
ENDMETHOD.
ENDCLASS.
" Call to Main Method
START-OF-SELECTION.
TRY.
lcl_main=>main( ).
CATCH lcl_exception INTO DATA(ex).
WRITE ex->get_text( ).
ENDTRY.这将产生以下输出:

当没有尝试捕获时使用:
" Call to Main Method
start-of-selection.
lcl_main=>main( ).这是输出:

发布于 2021-01-01 13:52:02
TL;DR : SAP不打算允许开发人员定制短转储。
“短转储”是在ABAP程序中发生意外错误时由ABAP内核生成的报告,即由于程序中的错误(通常是未察觉的异常,或不可捕获的错误)或系统故障(输入/输出资源、内存资源等)而产生的错误。
它旨在帮助开发人员分析错误的原因并纠正错误。
它并不是故意生成的,除非开发人员在理论上不可能实现,但实际发生了,如果发生这种情况,需要很多信息来分析,因此是一个短转储。
如果您真的打算生成带有消息的短转储,那么有两种方法:
MESSAGE 'message text' TYPE 'X'. (通常用于标准的SAP程序,特别是在条件表达式中的更新函数modules)RAISE SHORTDUMP ...或... THEN THROW SHORTDUMP ...中。这两者都存在于ABAP 7.53之后。例如,RAISE SHORTDUMP TYPE zcx_adk_exception EXPORTING message = 'Base_Exception_Error'.短转储将包含分析部分中的消息文本。
https://stackoverflow.com/questions/65510656
复制相似问题