Exception Handling via Class-Based Exception (IF_T100_MESSAGE)

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.

Create message class via transaction code SE91 and fill in messages required.

Maintain text ID for e.g. LOCK_BY_USER (1) and click the arrow button (2) as shown below.

After click arrow button (2), you will be shown with this screen.

Input code below after Line 9 to reference message class, number and placeholder.

CONSTANTS:
  BEGIN OF lock_by_user,
    msgid TYPE symsgid      VALUE 'ZMC',  "Refers to message class
    msgno TYPE symsgno      VALUE '001',  "Message class no.
    attr1 TYPE scx_attrname VALUE 'USER', "Placeholder
    attr2 TYPE scx_attrname VALUE '',
    attr3 TYPE scx_attrname VALUE '',
    attr4 TYPE scx_attrname VALUE '',
  END OF lock_by_user.

CONSTANTS:
  BEGIN OF db_error,
    msgid TYPE symsgid      VALUE 'ZMC',
    msgno TYPE symsgno      VALUE '002',
    attr1 TYPE scx_attrname VALUE '',
    attr2 TYPE scx_attrname VALUE '',
    attr3 TYPE scx_attrname VALUE '',
    attr4 TYPE scx_attrname VALUE '',
  END OF db_error.

This information shown below will be auto generated when you programmed code shown above.

The placeholder &USER& has to be declared in Attributes tab.

As you can see from the screenshot below,
‘ZMC’ refers to the message class.
‘001’ refers to message number.
‘USER’ refers to the placeholder. You can input up to 4 placeholders.

Here’s an example on how to use class-based exception in object-oriented approach.

***********************************************************************
* REPORT         : ZLCGE
***********************************************************************
* SAPFlash
***********************************************************************
REPORT zlcge.

*-----------------------------------------------------------------------
* 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 data has been locked by other user
      RAISE EXCEPTION TYPE zcx_exp
        EXPORTING
          textid = zcx_exp=>lock_by_user
          user   = sy-msgv1.
    ENDIF.
  ENDMETHOD.
ENDCLASS.

*-----------------------------------------------------------------------
* INITIALIZATION
*-----------------------------------------------------------------------
INITIALIZATION.
  IF NOT r_app IS BOUND.
    r_app = NEW cl_local_app( ).
  ENDIF.

*-----------------------------------------------------------------------
* START-OF-SELECTION
*-----------------------------------------------------------------------
START-OF-SELECTION.
  TRY.
      r_app->lock_table( ).
    CATCH zcx_exp INTO DATA(lr_exc).
      MESSAGE lr_exc TYPE 'S'.
  ENDTRY.

There are disadvantages with this approach using IF_T100_MESSAGE:

  1. Have to declare text ID and placeholder at Attributes tab for every exception message.
  2. Unable to pinpoint usage location of messages via where-used list from message class.

The improved and preferred version as of ABAP 7.50 is using IF_T100_DYN_MSG. You may refer to this guide Exception Handling via Class-Based Exception (IF_T100_DYN_MSG).