During my exception exploration I came to such conclusion, within local local exception class is sometime handy to have both input parameters, at first message with all its attributes or simple string. Below class fulfill both cases. Below exception class have two optional input parameters, so it's up to programmer what to choose. See below usage examples.
*----------------------------------------------------------------------*
* CLASS lcx_msg_exception DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcx_msg_exception DEFINITION
INHERITING FROM cx_static_check.
PUBLIC SECTION.
DATA: msgv1 TYPE symsgv READ-ONLY,
msgv2 TYPE symsgv READ-ONLY,
msgv3 TYPE symsgv READ-ONLY,
msgv4 TYPE symsgv READ-ONLY.
INTERFACES if_t100_message.
METHODS constructor
IMPORTING
iv_text TYPE clike OPTIONAL " simple text input
is_t100_key TYPE scx_t100key OPTIONAL. " message input
ENDCLASS. "lcx_msg_exception DEFINITION
*----------------------------------------------------------------------*
* CLASS lcx_msg_exception IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcx_msg_exception IMPLEMENTATION.
METHOD constructor.
super->constructor( ).
" simple string input
IF iv_text IS NOT INITIAL.
cl_message_helper=>set_msg_vars_for_clike( iv_text ).
if_t100_message~t100key-attr1 = sy-msgv1.
if_t100_message~t100key-attr2 = sy-msgv2.
if_t100_message~t100key-attr3 = sy-msgv3.
if_t100_message~t100key-attr4 = sy-msgv4.
ENDIF.
" message input
IF is_t100_key IS NOT INITIAL.
MOVE-CORRESPONDING is_t100_key TO if_t100_message~t100key.
ENDIF.
" special cases
IF ( if_t100_message~t100key-msgid IS INITIAL )
AND ( if_t100_message~t100key-msgno IS INITIAL )
AND ( if_t100_message~t100key-attr1 IS NOT INITIAL ).
if_t100_message~t100key-msgid = '00'.
if_t100_message~t100key-msgno = '001'.
ENDIF.
" set message attributes
msgv1 = if_t100_message~t100key-attr1.
msgv2 = if_t100_message~t100key-attr2.
msgv3 = if_t100_message~t100key-attr3.
msgv4 = if_t100_message~t100key-attr4.
if_t100_message~t100key-attr1 = 'MSGV1'.
if_t100_message~t100key-attr2 = 'MSGV2'.
if_t100_message~t100key-attr3 = 'MSGV3'.
if_t100_message~t100key-attr4 = 'MSGV4'.
ENDMETHOD. "constructor
ENDCLASS. "lcx_msg_exception IMPLEMENTATION
START-OF-SELECTION.
DATA: lx_oref TYPE REF TO lcx_msg_exception,
ls_msg TYPE scx_t100key.
* example 1 - simple success message
ls_msg-msgid = 'VL'.
ls_msg-msgno = '017'.
TRY.
RAISE EXCEPTION TYPE lcx_msg_exception
EXPORTING
is_t100_key = ls_msg.
CATCH lcx_msg_exception INTO lx_oref.
MESSAGE lx_oref TYPE 'S'.
ENDTRY.
* example 2 - error message with attributes
ls_msg-msgid = 'VL'.
ls_msg-msgno = '046'.
ls_msg-attr1 = '41000064'. " some VBELN
ls_msg-attr2 = 'IT007'. " some sy-uname user
TRY.
" ...some coding raising custom exception
RAISE EXCEPTION TYPE lcx_msg_exception
EXPORTING
is_t100_key = ls_msg.
CATCH lcx_msg_exception INTO lx_oref.
MESSAGE lx_oref TYPE 'E'.
ENDTRY.
* example 3 - simple error text message
DATA: lv_text TYPE string.
TRY.
" ...some coding raising custom exception
lv_text = 'Some ugly error occcured in program 1! Some stupid error occcured in program 2! Some silly error occcured in program 3!'.
RAISE EXCEPTION TYPE lcx_msg_exception
EXPORTING
iv_text = lv_text.
CATCH lcx_msg_exception INTO lx_oref.
MESSAGE lx_oref TYPE 'E'.
ENDTRY.
No comments:
Post a Comment