Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
AlwinPut
Active Participant
This blog post contains code templates for adding ZCX_RETURN3 exception raising code.

For more information on exception class ZCX_RETURN3, see blog post:

https://blogs.sap.com/2020/06/01/abap-exception-class-zcx_return3/

Template overview




ZEXC_ABAP_MESSAGE1 - CREATE_BY_SYSTEM_MESSAGE (>= 7.50)


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.
RAISE EXCEPTION TYPE zcx_return3
USING MESSAGE.
ENDIF.

ZEXC_ABAP_MESSAGE2 - CREATE_BY_SYSTEM_MESSAGE (< 7.50)


    IF sy-subrc <> 0.
DATA(lx_return) = NEW zcx_return3( ).
lx_return->add_system_message( ).
RAISE EXCEPTION lx_return.
ENDIF.

ZEXC_CUST_MESSAGE1 - CREATE_BY_SYSTEM_MESSAGE with custom message (>= 7.50)


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>
RAISE EXCEPTION TYPE zcx_return3
MESSAGE e001
"WITH iv_<variable>
.

ENDIF.

ZEXC_CUST_MESSAGE2 - CREATE_BY_SYSTEM_MESSAGE with custom message (< 7.50)


    IF sy-subrc <> 0.

"<English message text>
MESSAGE e001
"WITH iv_<variable>
INTO DATA(lv_dummy) ##NEEDED.

DATA(lx_return) = NEW zcx_return3( ).
lx_return->add_system_message( ).
RAISE EXCEPTION lx_return.

ENDIF.

ZEXC_BAPIRETURN_STRU - CREATE_BY_BAPIRETURN_STRUC


    DATA ls_return TYPE bapireturn.

"...BAPI call

DATA(lx_return) = NEW zcx_return3( ).
lx_return->add_bapireturn_struc( ls_return ).

IF lx_return->has_messages( ) = abap_true.
RAISE EXCEPTION lx_return.
ENDIF.

ZEXC_BAPIRETURN_TAB - CREATE_BY_BAPIRETURN_TABLE


    DATA lt_return TYPE zcx_return3=>gtt_bapireturn_t.

"...BAPI call

DATA(lx_return) = NEW zcx_return3( ).
lx_return->add_bapireturn_table( lt_return ).

IF lx_return->has_messages( ) = abap_true.
RAISE EXCEPTION lx_return.
ENDIF.

ZEXC_BAPIRET1_STRUC - CREATE_BY_BAPIRET1_STRUC


    DATA ls_return TYPE bapiret1.

"...BAPI call

DATA(lx_return) = NEW zcx_return3( ).
lx_return->add_bapiret1_struc( ls_return ).

IF lx_return->has_messages( ) = abap_true.
RAISE EXCEPTION lx_return.
ENDIF.

ZEXC_BAPIRET1_TAB - CREATE_BY_BAPIRET1_TABLE


    DATA:
lt_return TYPE bapiret1_tab.

"...BAPI call

DATA(lx_return) = NEW zcx_return3( ).
lx_return->add_bapiret1_table( lt_return ).

IF lx_return->has_messages( ) = abap_true.
RAISE EXCEPTION lx_return.
ENDIF.

ZEXC_BAPIRET2_STRUC - CREATE_BY_BAPIRET2_STRUC


    DATA ls_return TYPE bapiret2.

"...BAPI call

DATA(lx_return) = NEW zcx_return3( ).
lx_return->add_bapiret2_struc( ls_return ).

IF lx_return->has_messages( ) = abap_true.
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) = NEW zcx_return3( ).
lx_return->add_bapiret2_table( lt_return ).

IF lx_return->has_messages( ) = abap_true.
RAISE EXCEPTION lx_return.
ENDIF.

ZEXC_BDC_MESS_TABLE - CREATE_BY_BDC_MESSAGE_TABLE


    DATA lt_bdc_messages  TYPE zcx_return3=>gtt_bdc_messages.

* CALL TRANSACTION '???'
* USING lt_bdcdata
* MODE '?'
* UPDATE '?'
* MESSAGES INTO lt_bdc_messages.

DATA(lx_return) = NEW zcx_return3( ).
lx_return->add_bdc_table( lt_bdc_messages ).

IF lx_return->has_messages( ) = abap_true.
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(lx_return) = NEW zcx_return3( ).
lx_return->add_exception_object( lx_root ). "Change variable name

RAISE EXCEPTION lx_return.

ENDTRY.