ERPClimb logoERPClimb
OOP ABAP
ABAP Developmentintermediate 9 min read

OOP ABAP Part 4: Class-Based Exceptions

Preview · a free account unlocks the full lesson

Key takeaway

Handle errors the class-based way: TRY/CATCH/CLEANUP, RAISE EXCEPTION TYPE, the CX STATIC CHECK / CX DYNAMIC CHECK / CX NO CHECK hierarchy, custom exception classes with message texts, and how they compare with classic EXCEPTIONS and sy-subrc.

In this lessonShow sections

Visual overview

OOP ABAP Part 4: Class-Based Exceptions — learning sheetFlow: Create ZCX_FLIGHT_ERROR as in the code comments → Run the report: book a full flight, see the message text from ZFLIGHT → Add a CLEANUP block and observe when it runs. Key ideas: CLASSIC ERROR HANDLING (FOR COMPARISON); TRY / CATCH / CLEANUP; THE THREE CATEGORIES; CUSTOM EXCEPTION CLASS. Watch out: Classic EXCEPTIONS (sy-subrc) and class-based exceptions cannot be mixed in one method signature.ERPClimb.com / ABAP / LESSON 4/10 / LEARNING SHEETOOP ABAP Part 4: Class-Based Exceptions1 · HOW IT FLOWS1Create ZCX_FLIGHT_ERRORas in the code comments2Run the reportbook a full flight, see themessage text from ZFLIGHT3Add a CLEANUP block andobserve when it runs2 · KEY IDEAS EXPLAINEDClassic Error Handling (For Comparison)Function modules declare EXCEPTIONS not_found = 1and the caller checks sy-subrc after CALLFUNCTION. It still exists in every system (BAPIs…Try / Catch / CleanupCATCH blocks are checked in order — catch specificclasses before general ones (CX_ROOT last).INTO lx_error gives the exception object:get_text( ), previous, custom attributes.The Three CategoriesCX_STATIC_CHECK — the compiler checks: a methodmust declare it with RAISING or handle it. Use forexpected business errors (flight full, customer…CX_DYNAMIC_CHECK — declaration only checked atruntime; for errors callers can usually prevent(e.g. CX_SY_ZERODIVIDE).Custom Exception ClassSE24: create ZCX_FLIGHT_ERROR, superclassCX_STATIC_CHECK, tick "With MessageClass". Add interface IF_T100_MESSAGE texts…3 · REMEMBER · WATCH OUTRememberClass-based exceptions are objects: RAISE EXCEPTION TYPE zcx_… andhandle them with TRY … CATCH … ENDTRY.Three kinds: CX_STATIC_CHECK (must be declared/handled),CX_DYNAMIC_CHECK (runtime-checked), CX_NO_CHECK (system-like, never…Custom exception classes (ZCX_…) can carry T100 message texts andattributes.!Watch outClassic EXCEPTIONS (sy-subrc) andclass-based exceptions cannot bemixed in one method signature.4 · IN A REAL PROJECTA reusable pricing class issued MESSAGE type E when a condition was missing. When the class was later calledfrom a background interface and an OData service, the MESSAGE caused job terminations and unreadable HTTPerrors. The team replaced it with ZCX_PRICING_ERROR (CX_STATIC_CHECK with T100 texts). The report shows the…Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Class-based exceptions are objects: you raise one with RAISE EXCEPTION TYPE zcx_… and handle it with TRY … CATCH … ENDTRY. This matters because the methods built in Parts 1–3 simply wrote errors to the list, whereas reusable code needs to signal errors back to its caller instead.

For comparison, classic error handling works through function modules declaring EXCEPTIONS not_found = 1, with the caller checking sy-subrc after CALL FUNCTION. This style still exists throughout the system (BAPIs use a RETURN table instead), but it's easy to forget the sy-subrc check, and no further detail travels with the error.

The modern TRY / CATCH / CLEANUP construct looks like: TRY, call lo_service->book( … ); CATCH zcx_flight_error INTO lx_error, then MESSAGE lx_error TYPE 'I' DISPLAY LIKE 'E'; CLEANUP, which runs when the exception leaves this TRY block unhandled; ENDTRY. CATCH blocks are checked in order, so catch specific classes before general ones, leaving CX_ROOT last. Using INTO lx_error gives you the exception object itself, with get_text( ), a previous exception, and any custom attributes it carries.

Free account

Create a free account to keep reading

You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.

  • Full explanation
  • Code example
  • Real project scenario
  • Common mistakes
  • Best practices
  • Interview angle

One tap with Google works too. Every topic keeps its first lesson open to everyone.

Keep exploring

Referenced SAP objects

Reviewed reference pages for the objects this page mentions.

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.