Monday 19 October 2020

ABAP Exception Class ZCX_RETURN2 – Raise code templates

This blog post contains code templates for adding ZCX_RETURN2 exception raising code.

Template overview

◉ CREATE_BY_SYSTEM_MESSAGE (without MESSAGE statement)

◉ CREATE_BY_SYSTEM_MESSAGE (with MESSAGE statement for custom message)

◉ CREATE_BY_BAPIRETURN_STRUC

◉ CREATE_BY_BAPIRETURN_TABLE

◉ CREATE_BY_BAPIRET1_TABLE

◉ CREATE_BY_BAPIRET2_STRUC

◉ CREATE_BY_BAPIRET2_TABLE

◉ CREATE_BY_BDC_MESSAGE_TABLE

◉ CREATE_BY_EXCEPTION_OBJECT – catch for example CX_ROOT

ZEXC_ABAP_MESSAGE – CREATE_BY_SYSTEM_MESSAGE

This template is useful if a Function module or FORM routines returns a SY-SUBRC and has put the error message in the variables SY-MSGTY, SY-MSGID, SY-MSGNO, SY-MSGV1, SY-MSGV2, SY-MSGV3 and SY-MSGV4.

    IF sy-subrc <> 0.

      DATA(lx_return) = zcx_return2=>create_by_system_message( ).

      RAISE EXCEPTION lx_return.

    ENDIF.

ZEXC_CUST_MESSAGE – CREATE_BY_SYSTEM_MESSAGE with custom message

For example an ABAP statement returns SY-SUBRC <> 0.

Then you can raise the error by a custom message.

    IF sy-subrc <> 0.

      "<English message text>

      MESSAGE e001

        "WITH iv_<variable>

        INTO DATA(lv_dummy) ##NEEDED.

      DATA(lx_return) = zcx_return2=>create_by_system_message( ).

      RAISE EXCEPTION lx_return.

    ENDIF.

ZEXC_BAPIRETURN_STRU – CREATE_BY_BAPIRETURN_STRUC

    DATA ls_return TYPE bapireturn.

    "...BAPI call

    DATA(lx_return) =

      zcx_return2=>create_by_bapireturn_struc(

        is_return = ls_return ).

    IF lx_return IS NOT INITIAL.

      RAISE EXCEPTION lx_return.

    ENDIF.

ZEXC_BAPIRETURN_TAB – CREATE_BY_BAPIRETURN_TABLE

    DATA lt_return TYPE zcx_return2=>gtt_bapireturn_t.

    "...BAPI call

    DATA(lx_return) =

      zcx_return2=>create_by_bapireturn_table(

        it_return = lt_return ).

    IF lx_return IS NOT INITIAL.

      RAISE EXCEPTION lx_return.

    ENDIF.

ZEXC_BAPIRET1_STRUC – CREATE_BY_BAPIRET1_STRUC

    DATA ls_return TYPE bapiret1.

    "...BAPI call

    DATA(lx_return) =

      zcx_return2=>create_by_bapiret1_struc(

        is_return = ls_return ).

    IF lx_return IS NOT INITIAL.

      RAISE EXCEPTION lx_return.

    ENDIF.

ZEXC_BAPIRET1_TAB – CREATE_BY_BAPIRET1_TABLE

    DATA:

      lt_return           TYPE bapiret1_tab.

    "...BAPI call

    DATA(lx_return) =

      zcx_return2=>create_by_bapiret1_table(

        it_return = lt_return ).

    IF lx_return IS NOT INITIAL.

      RAISE EXCEPTION lx_return.

    ENDIF.

ZEXC_BAPIRET2_STRUC – CREATE_BY_BAPIRET2_STRUC

    DATA ls_return TYPE bapiret2.

    "...BAPI call

    DATA(lx_return) =

      zcx_return2=>create_by_bapiret2_struc(

        is_return = ls_return ).

    IF lx_return IS NOT INITIAL.

      RAISE EXCEPTION lx_return.

    ENDIF.

ZEXC_BAPIRET2_TAB – CREATE_BY_BAPIRET2_TABLE

    DATA:

      lt_return TYPE STANDARD TABLE OF bapiret2.

    "...BAPI call

    DATA(lx_return) =

      zcx_return2=>create_by_bapiret2_table(

        it_return = lt_return ).

    IF lx_return IS NOT INITIAL.

      RAISE EXCEPTION lx_return.

    ENDIF.

ZEXC_BDC_MESS_TABLE – CREATE_BY_BDC_MESSAGE_TABLE

    DATA lt_bdc_messages  TYPE zcx_return2=>gtt_bdc_messages.

*    CALL TRANSACTION '???'

*      USING lt_bdcdata

*      MODE '?'

*      UPDATE '?'

*      MESSAGES INTO lt_bdc_messages.

    DATA(lx_return) =

      zcx_return2=>create_by_bdc_table(

        it_bdc_messages = lt_bdc_messages ).

    IF lx_return IS NOT INITIAL.

      RAISE EXCEPTION lx_return.

    ENDIF.

ZEXC_OO_EXCEPTION – CREATE_BY_EXCEPTION_OBJECT

You can catch CX_ROOT or more specific exception classes.

Make for every exception code location a new error number. So the where used list is always usable.

Also come up with a logical error name <Short error name>. Don’t make it too long, because the rest of the line is needed for the message of LX_ROOT. The total message length is 220 characters. (See structure BAPIRET2 variable MESSAGE.)

The method CREATE_BY_EXCEPTION_OBJECT will cut the message text of LX_ROOT into 4 pieces (&1 &2 &3 en &4) as described in the comment of the code.

    TRY.

        "...method call

      CATCH cx_root INTO DATA(lx_root). "TODO: change exception class

        "<Short error name>: &1&2&3&4

        MESSAGE e001                    "Todo: change error number

          INTO DATA(lv_dummy).

        "Method will split message text into &1 &2 &3 &4

        DATA(lr_return) =

          zcx_return2=>create_by_exception_object( lx_root ). "Change variable name

        RAISE EXCEPTION lr_return.

    ENDTRY.

No comments:

Post a Comment