3.5Bloom · UNot started

Class-based exceptions

Reading depth

What you'll learn

Inherit CX_STATIC_CHECK (declared, compiler-enforced — the Clean ABAP default) over CX_DYNAMIC_CHECK or CX_NO_CHECK, use IF_T100_MESSAGE for texts, and prefer ASSERT for can't-happen cases.

  • CX_STATIC_CHECK: declared in the signature, compiler-enforced — the Clean ABAP default.
  • CX_DYNAMIC_CHECK: not declared, checked at runtime.
  • CX_NO_CHECK: propagates silently, no declaration, no caller obligation.

Clean ABAP uses class-based exceptions exclusively, and the category you inherit from sets the contract. CX_STATIC_CHECK must be declared in a method's signature and the compiler enforces that callers handle or propagate it — this compile-time contract is exactly why it is the Clean ABAP default. CX_DYNAMIC_CHECK need not be declared (the check is at runtime), and CX_NO_CHECK propagates silently with no declaration and no obligation on the caller.

Exception texts come from the IF_T100_MESSAGE interface: implement it on your CX_ class and store a T100 message key so the exception carries a proper, translatable message rather than an ad-hoc string. The constructor pattern wires textid to if_t100_message~t100key, defaulting to the class's default text when none is supplied.

For situations that genuinely cannot happen — a state the surrounding logic has already guaranteed — prefer an ASSERT over a raised exception. An ASSERT documents the invariant and fails fast in test, without forcing every caller to handle a 'can't-happen' case; reserve CX_NO_CHECK for the rare cases where assertion is not appropriate.

Key points

  • CX_STATIC_CHECK: declared in the signature, compiler-enforced — the Clean ABAP default.
  • CX_DYNAMIC_CHECK: not declared, checked at runtime.
  • CX_NO_CHECK: propagates silently, no declaration, no caller obligation.
  • Implement IF_T100_MESSAGE for proper, translatable exception texts.
  • Prefer ASSERT for can't-happen invariants over a raised exception.

Examples

AfterA static-check exception class

Inheriting from cx_static_check makes the exception compile-enforced; if_t100_message carries the text.

ABAPclass zcx_order_invalid definition
  public
  inheriting from cx_static_check
  create public.
  public section.
    interfaces if_t100_message.
endclass.
ASSERT for a can't-happen invariant

When prior logic guarantees a non-empty key, an ASSERT documents the invariant instead of a raised exception every caller must handle.

ABAPassert lv_key is not initial.

Source notes: clean-core-curriculum §3.5

Ask Claude

Build a prompt from this lesson + your question and open a fresh Claude chat with it pre-filled — handy for adapting a before/after pattern to your own object.