Handling Exception and use of CX_ROOT directly and subclasses

It is not advisable to use CX_ROOT directly and you would require using one of its direct subclasses. The CX_ROOT is the root class for all exception classes in SAP ABAP, but working with its subclasses provides better control and handling mechanisms. Also, the propagation depends upon the exception subclass hierarchy.

Exception Subclass Types

There are three main types of exception subclasses, each with different propagation and handling behaviors ?

  • Subclasses of CX_STATIC_CHECK ? these do not propagate automatically. You will need to do the handling yourself or there will be a syntax error in the program.
  • Subclasses of CX_DYNAMIC_CHECK ? these do not require any handling. But the program that does not handle the exception will be aborted with these subclasses.
  • Subclasses of CX_NO_CHECK ? These will get propagated automatically if the exception is not handled.

Example

Here's an example demonstrating proper exception handling using CX_ROOT subclasses ?

DATA: lv_result TYPE i,
      lv_divisor TYPE i VALUE 0.

TRY.
    " This will raise CX_SY_ZERODIVIDE (subclass of CX_DYNAMIC_CHECK)
    lv_result = 10 / lv_divisor.
    
  CATCH cx_sy_zerodivide INTO DATA(lo_exception).
    " Handle the specific exception
    WRITE: 'Division by zero error occurred:',
           lo_exception->get_text( ).
    
  CATCH cx_root INTO DATA(lo_root_exception).
    " Catch any other exception
    WRITE: 'General error occurred:',
           lo_root_exception->get_text( ).
    
ENDTRY.

In this example, we catch a specific exception first (CX_SY_ZERODIVIDE) and then use CX_ROOT as a fallback to catch any other unexpected exceptions.

Conclusion

Always use specific subclasses of CX_ROOT rather than CX_ROOT directly for better exception handling and program control. Understanding the propagation behavior of different exception types helps in writing more robust ABAP programs.

Updated on: 2026-03-13T19:12:51+05:30

463 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements