Exceptions are used to handle errors arised during program execution. It can be used in object-oriented design as well as all processing blocks. Class-based exceptions are usually defined globally in Class Builder (SE24).
This guide below show the steps required to create class-based exceptions.
Go to transaction code SE24. Input ‘Object type’ with prefix ZCX_ , in this case I’ll use ZCX_EXP. Next, click Create.
Fill in information and settings for popup as shown below. Click Save. Choose Package and assign to transport request according to project guidelines.
Click Interfaces tab. If your system are already using ABAP 7.50, interface IF_T100_DYN_MSG
will be auto populated as shown below.
Click Alias tab. Populate visibility and alias of IF_T100_DYN_MSG~MSGTY
as shown below.
Create message class via transaction code SE91 and fill in messages required.
In contrast to class-based exception (IF_T100_MESSAGE), we do not need to create text ID for each exception message nor placeholders for variables. That’s all for the exception class.
Local Class
Here’s an example on how to use class-based exception in program using local class object-oriented approach.
*********************************************************************** * REPORT : ZLCGE_IF_T100_DYN_MSG *********************************************************************** * SAPFlash *********************************************************************** REPORT zlcge_if_t100_dyn_msg. *----------------------------------------------------------------------- * CLASS *----------------------------------------------------------------------- CLASS: cl_local_app DEFINITION DEFERRED. DATA: r_app TYPE REF TO cl_local_app. *----------------------------------------------------------------------* * CLASS cl_local_app DEFINITION *----------------------------------------------------------------------* * Class Definition for cl_local_app *----------------------------------------------------------------------* CLASS cl_local_app DEFINITION FINAL. PUBLIC SECTION. METHODS: lock_table RAISING zcx_exp. ENDCLASS. *----------------------------------------------------------------------* * CLASS cl_local_app IMPLEMENTATION *----------------------------------------------------------------------* * Class Implementation for cl_local_app *----------------------------------------------------------------------* CLASS cl_local_app IMPLEMENTATION. *&---------------------------------------------------------------------* *& LOCK_TABLE *&---------------------------------------------------------------------* * Lock table *----------------------------------------------------------------------* * !CX! Raise exception *----------------------------------------------------------------------* METHOD lock_table. CALL FUNCTION 'ENQUEUE_EVVBAKE' EXPORTING mode_vbak = 'X' mandt = sy-mandt EXCEPTIONS foreign_lock = 1 system_failure = 2 OTHERS = 3. IF sy-subrc <> 0. "Issue message table has been locked by other user RAISE EXCEPTION TYPE zcx_exp MESSAGE s001(zmc) WITH sy-msgv1. ENDIF. ENDMETHOD. ENDCLASS. *----------------------------------------------------------------------- * INITIALIZATION *----------------------------------------------------------------------- INITIALIZATION. IF NOT r_app IS BOUND. r_app = NEW cl_local_app( ). ENDIF. START-OF-SELECTION. TRY. r_app->lock_table( ). CATCH zcx_exp INTO DATA(lr_exc). MESSAGE lr_exc TYPE lr_exc->msgty. ENDTRY.
Global Class
Here’s an example on how to use class-based exception in program using global class object-oriented approach.
First, create the controller class with method (1) and codes as below.
METHOD lock_table. CALL FUNCTION 'ENQUEUE_EVVBAKE' EXPORTING mode_vbak = 'X' mandt = sy-mandt EXCEPTIONS foreign_lock = 1 system_failure = 2 OTHERS = 3. IF sy-subrc <> 0. "Issue message data has been locked by other user RAISE EXCEPTION TYPE zcx_exp MESSAGE s001(zmc) WITH sy-msgv1. ENDIF. ENDMETHOD.
Next, click on Exceptions (2). Input the exception class created earlier as shown below. Save and activate this class.
Create a program to test it.
*********************************************************************** * REPORT : ZGCGE_IF_T100_DYN_MSG *********************************************************************** * SAPFlash *********************************************************************** REPORT zgcge_if_t100_dyn_msg. DATA: r_app TYPE REF TO zgb_cl_app. *----------------------------------------------------------------------- * INITIALIZATION *----------------------------------------------------------------------- INITIALIZATION. IF NOT r_app IS BOUND. r_app = NEW zgb_cl_app( ). ENDIF. START-OF-SELECTION. TRY. r_app->lock_table( ). CATCH zcx_exp INTO DATA(lr_exc). MESSAGE lr_exc TYPE lr_exc->msgty. ENDTRY.
I’ll now use where-used list in message class zmc
message number 001 to pinpoint usage location of this message. From the screenshot below, you can see the where-used results. It is being used at global class zgb_cl_app
method lock_table
and in local class of program zlcge_if_t100_dyn_msg
.
As you can see, there are advantages with this approach using IF_T100_DYN_MSG
:
- Not required to declare text ID and placeholder at Attributes tab for every exception message.
- Able to pinpoint usage location of messages via where-used list from message class.