Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Exception handling for event handler method

former_member333493
Participant
0 Likes
3,881

I have an event handler DELETE_LOCAL_AFTER_COMMIT whose code is as follows:

METHOD delete_local_after_commit. "line no. 1
LOOP AT gt_del_obj_tab INTO DATA(ls_del_obj_tab).
TRY.
CALL METHOD cl_ilm_tadir_api=>tadir_for_local_object
EXPORTING
iv_object_category = ls_del_obj_tab-object_category
iv_object_type = ls_del_obj_tab-object_type
iv_delflag = 'X'.
CATCH cx_ilm_tadir_api INTO DATA(lx_error).
RAISE EXCEPTION lx_error. "line no. 10
ENDTRY.
ENDLOOP.
CLEAR gt_del_obj_tab.
ENDMETHOD.

The piece of code calling event handler is as follows:

TRY.
CALL METHOD cl_ilm_tadir_api=>delete_local_after_commit.
CATCH cx_ilm_tadir_api INTO DATA(lx_error). "line no. 18

ENDTRY.

However the exception raised in line no. 10 is not caught in line no. 18. What could be going wrong? (Since my method is an event handler, I cannot even add an exception class to it)

6 REPLIES 6
Read only

Sandra_Rossi
Active Contributor
0 Likes
2,755

I doubt that an event handler can be called with CALL METHOD... An event handler is to be "called" with RAISE EVENT... (indirectly-loose coupling)

Read only

former_member333493
Participant
0 Likes
2,755

In the actual scenario, it is a RAISE EVENT which triggers my event handler. But this in my Local Test Classes where I am forcefully trying to call my event handler DELETE_LOCAL_AFTER_COMMIT (without doing a SET HANDLER...)

Read only

Sandra_Rossi
Active Contributor
0 Likes
2,755

Probably you inherited your exception class from CX_STATIC_CHECK or CX_DYNAMIC_CHECK, it is then wrapped into an exception of class CX_SY_NO_HANDLER, because [CLASS-]EVENTS <event> cannot declare RAISING <exception-classes>.

It's explained in the ABAP documentation - Class-Based Exceptions in Event Handlers.

Only an exception class derived from CX_NO_CHECK is not wrapped.

Below is a test code, which returns the caught exceptions in the following 3 variables:

The test code (run it with Ctrl+Shift+F10):

REPORT zzsro_test6b.

CLASS lcl_situation DEFINITION.
  PUBLIC SECTION.
    EVENTS something_happened EXPORTING VALUE(exception) TYPE REF TO cx_root.
ENDCLASS.

CLASS lcl_situation IMPLEMENTATION.
ENDCLASS.

CLASS lcl_observer DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS if_something_happened FOR EVENT something_happened OF lcl_situation IMPORTING exception.
ENDCLASS.

CLASS lcl_observer IMPLEMENTATION.
  METHOD if_something_happened.
    RAISE EXCEPTION exception.
  ENDMETHOD.
ENDCLASS.

CLASS lcx_no_check DEFINITION INHERITING FROM cx_no_check.
ENDCLASS.

CLASS lcx_static_check DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.

CLASS lcx_dynamic_check DEFINITION INHERITING FROM cx_dynamic_check.
ENDCLASS.

CLASS lcl_app DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS lcl_app IMPLEMENTATION.
  METHOD main.
    lcl_observer=>if_something_happened( NEW lcx_no_check( ) ).
  ENDMETHOD.
ENDCLASS.

CLASS ltc_main DEFINITION
      FOR TESTING
      DURATION SHORT
      RISK LEVEL HARMLESS.
  PRIVATE SECTION.
    METHODS test FOR TESTING.
ENDCLASS.

CLASS ltc_main IMPLEMENTATION.
  METHOD test.
    TRY.
        lcl_observer=>if_something_happened( NEW lcx_no_check( ) ).
      CATCH cx_root INTO DATA(lx_for_no_check).
    ENDTRY.
    TRY.
        lcl_observer=>if_something_happened( NEW lcx_static_check( ) ).
      CATCH cx_root INTO DATA(lx_for_static_check).
    ENDTRY.
    TRY.
        lcl_observer=>if_something_happened( NEW lcx_dynamic_check( ) ).
      CATCH cx_root INTO DATA(lx_for_dynamic_check).
    ENDTRY.
    BREAK-POINT. " DEBUGGER HERE
  ENDMETHOD.
ENDCLASS.
Read only

0 Likes
2,755

So if the exceptions lcx_no_check, lcx_static_check and lcx_dynamic_check were raised with messages from a message class, it would not be possible to extract the corresponding msgid and msgno from the variables lx_for_no_check, lx_for_static_check and lx_for_dynamic_check, right?

Read only

0 Likes
2,755

You ask a new question, completely unrelated to the previous one, and unrelated to my answer.

It's not required that an exception class is to be related to a message class. If you create your exception class with message(s) from a message class, there's no issue to retrieve the corresponding msgid and msgno. If you need more information, please ask a new question with more details.

Read only

Sandra_Rossi
Active Contributor
0 Likes
2,755

As you said, I was wrong: an event handler can be called with CALL METHOD.

I posted a detailed answer which explains what probably happened in your case.