# ABAP Custom Exception Class Generator

> Source: https://netray.co/tools/abap-custom-exception-class-generator
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: Free Tools

## Short answer

Fill in a class name, message class and attribute and get a ready-to-paste class-based exception with a TRY/CATCH usage example.

## Category

developer

## Erp

SAP ABAP

## Audience

- ABAP developer
- SAP technical consultant

## Tool

**Kind:** template

**Output:** code

**Language:** abap

**Fields:**

#### Exception class name

**Key:** exc_class

**Type:** text

**Default:** zcx_order_error

#### Superclass

**Key:** base_class

**Type:** select

**Options:**

- cx_static_check
- cx_dynamic_check
- cx_no_check

**Default:** cx_static_check

#### Message class

**Key:** msgid

**Type:** text

**Default:** ZORDER

#### Message number

**Key:** msgno

**Type:** text

**Default:** 001

#### Context attribute name

**Key:** attr_name

**Type:** text

**Default:** order_id

#### Context attribute type

**Key:** attr_type

**Type:** text

**Default:** vbeln_va

#### Class that raises it

**Key:** raiser_class

**Type:** text

**Default:** zcl_order_service

#### Raising method name

**Key:** raiser_method

**Type:** text

**Default:** release_order

**Blocks:**

#### Exception class definition

**Body:** CLASS {{exc_class}} DEFINITION
  PUBLIC
  INHERITING FROM {{base_class}}
  FINAL
  CREATE PUBLIC.

  PUBLIC SECTION.
    INTERFACES if_t100_message.

    CONSTANTS:
      BEGIN OF default_error,
        msgid TYPE symsgid VALUE '{{msgid}}',
        msgno TYPE symsgno VALUE '{{msgno}}',
        attr1 TYPE scx_attrname VALUE '{{attr_name}}',
        attr2 TYPE scx_attrname VALUE '',
        attr3 TYPE scx_attrname VALUE '',
        attr4 TYPE scx_attrname VALUE '',
      END OF default_error.

    DATA {{attr_name}} TYPE {{attr_type}} READ-ONLY.

    METHODS constructor
      IMPORTING
        textid       LIKE if_t100_message=>t100key OPTIONAL
        previous     LIKE previous OPTIONAL
        {{attr_name}} TYPE {{attr_type}} OPTIONAL.
ENDCLASS.

#### Exception class implementation

**Body:** CLASS {{exc_class}} IMPLEMENTATION.
  METHOD constructor ##ADD_SUPER_CALL.
    CALL METHOD super->constructor
      EXPORTING
        previous = previous.
    me->{{attr_name}} = {{attr_name}}.
    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

**Body:** METHOD {{raiser_method}}.
  " importing iv_{{attr_name}} TYPE {{attr_type}}
  " raising {{exc_class}}
  IF iv_{{attr_name}} IS INITIAL.
    RAISE EXCEPTION TYPE {{exc_class}}
      EXPORTING
        {{attr_name}} = iv_{{attr_name}}.
  ENDIF.
ENDMETHOD.

#### Catching the exception

**Body:** TRY.
    NEW {{raiser_class}}( )->{{raiser_method}}( iv_{{attr_name}} = lv_{{attr_name}} ).
  CATCH {{exc_class}} INTO DATA(lx_error).
    DATA(lv_text) = lx_error->get_text( ).
    MESSAGE lv_text TYPE 'E'.
ENDTRY.

**Notes:** 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.

## Content

**Intro:** 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.

**Sections:**

#### How to use the generator

**Body:** 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

**Body:** 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

**Body:** 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

**Body:** 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.

**Cta:** 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.

**Faq:**

#### What does IF_T100_MESSAGE do?

**Answer:** 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?

**Answer:** 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?

**Answer:** 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?

**Answer:** 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.

## Search terms

abap custom exception class, abap cx_static_check example, abap if_t100_message example, abap class based exceptions, abap try catch example, abap raise exception type, abap zcx class template, abap exception with message class

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
