Example: Exception Handling in Global Class


In this example we have a global ABAP class called Z_CL_KUNDER. The class has the method KUNDE_GETTABLE, which returns an internal table. If there are no entries in the table, the an excpetion should be raised and catched by the ABAP program that uses the class.

1. Create an excepetion handler class

Create an excpetion handler class in SE24 called ZCX_CL_KUNDER_NO_ENTRIES.

Go to the textx tab and locate the exception ID ZCX_CL_KUNDER_NO_ENTRIES, which has been automatically created, when you created the class, and inster the text No entries found:

Generate the class

2. Use the class in the ABAP class

Please refere to the class created in Example: Converting a local class in a report to a global class.

The following changes should be made to the class:

Select method KUNDE_GETTABLE in the Methods tab, and press the Excpetions button:


After you have pressed the Exceptions button, enter the name of the global exception-handler call created above and a description:

Go back and double-click on the method KUNDE_GETTABLE and change the code for the method as shown below:

METHOD kunde_gettable.
   FROM zkunder
   INTO TABLE kunde_table.
  IF sy-subrc <> 0.
    RAISE exception type ZCX_CL_KUNDER_NO_ENTRIES.

This will raise the exception ZCX_CL_KUNDER_NO_ENTRIES if no ientries are found in zkunder.

3. Catch the excepetion in an ABAP report

In END-OF-SELECIOn the method kunde_gettable in class z_cl_kunder is called. If the excepetion zcx_cl_kunder_no_entries are raised by the method, the excpetion is catched an the error message associated with the exception in the excpetion class is displayed.

Note: The most interesting pieces of code, are coloured red.

REPORT  ztest_object                            .

DATA: g_kunde TYPE zkunder,
      gi_kunder TYPE STANDARD TABLE OF zkunder,
      o_kunde TYPE REF TO z_cl_kunder, "Global class
      mes TYPE string.

DATA o_exception TYPE REF TO zcx_cl_kunder_no_entries.

* Create new entry in ZKUNDER
  CLEAR g_kunde.
  g_kunde-zzkunnr = '3'.
  g_kunde-zzkunnavn = 'Baloo export'.

  CREATE OBJECT o_kunde.
  CALL METHOD o_kunde->kunde_addnew
      pkunde = g_kunde.

  CLEAR g_kunde.

* Display all entries in ZKUNDER and dispay an error message if no
* entries are found
      CALL METHOD o_kunde->kunde_gettable
          kunde_table = gi_kunder.

      LOOP AT gi_kunder INTO g_kunde.
        WRITE: / g_kunde-zzkunnr, g_kunde-zzkunnavn.

    CATCH zcx_cl_kunder_no_entries INTO o_exception.
      mes = o_exception->get_text( ).
      MESSAGE mes TYPE 'I'.


If the exception is raised, the following message will be shown:






Converted from CHM to HTML with chm2web Standard 2.7 (unicode)