Class-based Exceptions I – Basics

By | December 19, 2011 | ABAP Objects, Exceptions, OO Concepts | 70,683 | 3

Class based exceptions are realized based on the instances of the exception class. Lets explore them.

Checkout all post related to Exception Raising & handling:


Whenever a exception is triggered, an exception object is created. This object would contain more information on the error condition. This exception can be caught and handled using the TRY .. CATCH.. ENDTRY block.

TRY.. ENDTRY block decalres a protective section in which we can CATCH one or more exceptions. If Caller is also interested in knowing about the error situation, caller should grabe the generated exception object, using the CATCH statement addition INTO. When INTO is specified, exception object would be assigned into the variable.

Within one TRY .. ENDTRY statment, we can have more than one CATCH statement. Since we can catch more than one exception, they should be caught in More-specific to generic class sequence. If we don’t CATCH them in this sequence than, system would generate an syntax error mentioning that we must declare exceptions in ascending order.

Since all the exception classes are inherited from CX_ROOT, We can use the method GET_TEXT and GET_LONG_TEXT to know more about why exception was raised.


For this demo, we’ll create a local exception class inherited from CX_STATIC_CHECK. More on CX_STATIC_CHECK and other classes in coming post. We’ll raise this exception and catch the exception. We also check on how to catch standard exception.

Class Based Exception- Raising & handling

REPORT znp_oo_exce_demo.
CLASS lcx_mandatory_missing DEFINITION
  INHERITING FROM cx_static_check.
ENDCLASS.                    "lcx_mandatory_missing  DEFINITIO
CLASS lcl_test_exceptions DEFINITION.
    METHODS: calculate IMPORTING iv_num1 TYPE i OPTIONAL
                               iv_num2 TYPE i OPTIONAL
                     RETURNING value(rv_calc) TYPE i
                     RAISING   lcx_mandatory_missing
    METHODS: do_div RAISING cx_sy_arithmetic_error.
    METHODS: do_check RAISING  lcx_mandatory_missing.
    DATA: v_num1 TYPE i,
          v_num2 TYPE i,
          v_result TYPE i.
ENDCLASS.                    "lcl_test_exceptions DEFINITION
CLASS lcl_test_exceptions IMPLEMENTATION.
  METHOD calculate.
    v_num1 = iv_num1.
    v_num2 = iv_num2.
    me->do_check( ).
    me->do_div( ).
    rv_calc = v_result.
  ENDMETHOD.                    "calculate
  METHOD do_check.
    IF v_num1 IS INITIAL.
      RAISE EXCEPTION TYPE lcx_mandatory_missing.
  ENDMETHOD.                    "do_check
  METHOD do_div.
      v_result = v_num1 DIV v_num2.
  ENDMETHOD.                    "do_sum
ENDCLASS.                    "lcl_test_exceptions IMPLEMENTATION
  DATA: lo_obj TYPE REF TO lcl_test_exceptions.
  DATA: lo_exc_root TYPE REF TO cx_root.
  DATA: lv_msg TYPE string.
  TRY .
      lo_obj->calculate( iv_num1 = 10 ).
    CATCH lcx_mandatory_missing.
      WRITE: /  'Mandatory Parameter is missing'.
    CATCH cx_root INTO lo_exc_root.
      lv_msg = lo_exc_root->get_text( ).
      WRITE: / lv_msg.

This program will catch the exception, CX_SY_ZERODIVIDE becasue we are trying to devide with initial V_NUM2 in the method DO_DIV( ). If we change the value IV_NUM1 with 0, it would raise our custom exception LCX_MANDATORY_MISSING from the method DO_CHECK( ). Since both of them are declared in the method signature of CALCULATE( ), they both would be propogated back to caller program without any problem.

In next posts, we’ll check the design time consideration as well as run-time consideration of exceptions with Flow diagram.

Checkout all post related to Exception Raising & handling:

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{273 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 273 articles.

Load comments


  • [...] & Handling Non-class based exceptions – I Raising & Handling Non-class based exceptions – II Class-based Exceptions I – Basics Class-based Exceptions II – Design Time consideration Class based Exceptions III – Runtime [...]

  • Kesav

    Thanks for considering the request and posting this content :)

  • Naimesh Patel

    I’m glad that you like it..!

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.