Developer ToolsFree generatorPDF, Excel, Word exportSAP ABAP

ABAP Custom Exception Class Generator

Class-based exceptions replaced the old classic exceptions of function modules, and a clean exception class makes errors easy to catch, translate and log. This generator produces the class definition, the constructor, a raising example and a TRY/CATCH block that uses a T100 message so the text comes from SE91.

Fill in

Generated code
Download:

Exception class definition

CLASS zcx_order_error DEFINITION
  PUBLIC
  INHERITING FROM cx_static_check
  FINAL
  CREATE PUBLIC.

  PUBLIC SECTION.
    INTERFACES if_t100_message.

    CONSTANTS:
      BEGIN OF default_error,
        msgid TYPE symsgid VALUE 'ZORDER',
        msgno TYPE symsgno VALUE '001',
        attr1 TYPE scx_attrname VALUE 'order_id',
        attr2 TYPE scx_attrname VALUE '',
        attr3 TYPE scx_attrname VALUE '',
        attr4 TYPE scx_attrname VALUE '',
      END OF default_error.

    DATA order_id TYPE vbeln_va READ-ONLY.

    METHODS constructor
      IMPORTING
        textid       LIKE if_t100_message=>t100key OPTIONAL
        previous     LIKE previous OPTIONAL
        order_id TYPE vbeln_va OPTIONAL.
ENDCLASS.

Exception class implementation

CLASS zcx_order_error IMPLEMENTATION.
  METHOD constructor ##ADD_SUPER_CALL.
    CALL METHOD super->constructor
      EXPORTING
        previous = previous.
    me->order_id = order_id.
    CLEAR me->textid.
    IF textid IS INITIAL.
      if_t100_message~t100key = default_error.
    ELSE.
      if_t100_message~t100key = textid.
    ENDIF.
  ENDMETHOD.
ENDCLASS.

Raising the exception

METHOD release_order.
  " importing iv_order_id TYPE vbeln_va
  " raising zcx_order_error
  IF iv_order_id IS INITIAL.
    RAISE EXCEPTION TYPE zcx_order_error
      EXPORTING
        order_id = iv_order_id.
  ENDIF.
ENDMETHOD.

Catching the exception

TRY.
    NEW zcl_order_service( )->release_order( iv_order_id = lv_order_id ).
  CATCH zcx_order_error INTO DATA(lx_error).
    DATA(lv_text) = lx_error->get_text( ).
    MESSAGE lv_text TYPE 'E'.
ENDTRY.

Create the message {{msgno}} in message class {{msgid}} in SE91 with the text &1 so the attribute is substituted into the message. A static check exception must be declared in the RAISING clause of every method that can raise it. Verify the syntax against your ABAP release; the shape shown is valid for 7.40 and later.

Standardise ABAP error handling

Optional: a short conversation about reviewing exception handling across your custom code with a private AI assistant.

Optional. The tool and every export work without this. No spam.

How to use the generator

Enter the Z class name, the message class and number you will create in SE91, and the name and type of the context attribute, such as the order number. The tool fills four code blocks: the class definition, the implementation, the code that raises it and the code that catches it. Copy each into SE24, an ADT project or a global class source, then activate. Replace the placeholder method names with your own.

Choosing the superclass

CX_STATIC_CHECK must be declared in the RAISING clause or caught, and the compiler enforces it, which suits business errors the caller is expected to handle. CX_DYNAMIC_CHECK is checked at runtime, and CX_NO_CHECK needs no declaration and is meant for situations the caller cannot reasonably handle. A good default for business rule failures is CX_STATIC_CHECK. Keep a small hierarchy of your own, for example one base exception per application area.

T100 messages and context attributes

Implementing IF_T100_MESSAGE links the exception to a message in SE91, so you get translated texts and the same text in logs and screens. The attributes listed in the default text id constant are substituted into the placeholders &1 to &4. Keep attribute data public and read-only so catchers can read the order number or other context without parsing text. Always chain PREVIOUS when you wrap a lower level error, so the original cause is not lost.

Common mistakes

Developers often forget to add the exception to the RAISING clause, catch the superclass CX_ROOT too broadly, or lose the previous exception. Another mistake is building the message text by concatenation, which cannot be translated. Do not use exceptions for normal control flow in tight loops. Finally, test the class in an ABAP Unit test that checks the text and the attribute value, not just that something was raised.

Frequently Asked Questions

What does IF_T100_MESSAGE do?

It lets an exception class point to a message in a message class defined in SE91. The exception then returns a translatable text with attributes substituted into the placeholders. It is the standard way to give class-based exceptions proper user messages.

Should I use CX_STATIC_CHECK or CX_NO_CHECK?

Use CX_STATIC_CHECK for errors the caller can reasonably handle, since the compiler forces them to be declared. Use CX_NO_CHECK for fatal conditions the caller cannot recover from, which then propagate without declaration. Most business exceptions are static check.

Why do I need the previous parameter?

PREVIOUS chains the lower-level exception so the root cause stays available through the exception chain. Without it a wrapper hides the original failure, and debugging has to start from your wrapper only. Pass it whenever you catch and re-raise.

Can I use this code in older releases?

The class pattern works from about release 7.02 onward, and the inline declarations used in the catch example need 7.40 or later. For older systems declare the variable with DATA beforehand and call lx_error->get_text( ) the same way.

If you want help standardising error handling across a large custom code base, Netray can discuss private AI-assisted code review that runs against your own repository.