ERPClimb logoERPClimb
ABAP Core
ABAP Developmentbeginner 9 min read

ABAP Core Part 9: TRY, CATCH and Exception Handling Basics

Preview · a free account unlocks the full lesson

Key takeaway

Handle errors with class-based exceptions: TRY/CATCH/CLEANUP, exception class hierarchy (static, dynamic, no-check), catching specific exceptions, getting texts, raising your own exceptions, and not swallowing errors.

In this lessonShow sections

Visual overview

ABAP Core Part 9: TRY, CATCH and Exception Handling Basics — learning sheetFlow: Run the division and table expression examples → Create an exception class for "flight full" (static check) → Raise it in a method when a flight is full; catch it in the report and show a message. Key ideas: TRY / CATCH / CLEANUP; THE EXCEPTION HIERARCHY; SYSTEM EXCEPTIONS YOU WILL MEET; RAISING YOUR OWN EXCEPTIONS. Watch out: Never catch cx_root and do nothing — swallowed errors become data problems later.ERPClimb.com / ABAP / LESSON 9/10 / LEARNING SHEETABAP Core Part 9: TRY, CATCH and Exception Handling Basics1 · HOW IT FLOWS1Run the division andtable expression examples2Create an exception classfor "flightfull" (static check)3Raise it in a method whena flight is full; catchit in the report and show…2 · KEY IDEAS EXPLAINEDTry / Catch / CleanupTRY.The Exception HierarchyCX_ROOT — top.CX_STATIC_CHECK — must be declared in the methodsignature (RAISING) or handled; the syntax checkenforces it. Use for expected business errors the…System Exceptions You Will MeetCX_SY_ZERODIVIDE, CX_SY_CONVERSION_NO_NUMBER,CX_SY_ITAB_LINE_NOT_FOUND (table expressionwithout line), CX_SY_OPEN_SQL_DB…Raising Your Own ExceptionsCreate an exception class ZCX_FLIGHT_ERRORinheriting from CX_STATIC_CHECK, ideally withmessage-class texts (interface IF_T100_MESSAGE /…3 · REMEMBER · WATCH OUTRememberTRY … CATCH cx_… INTO lx_ref. … ENDTRY handles class-based exceptions(declare lx_ref TYPE REF TO cx_…).Three kinds: CX_STATIC_CHECK (must be declared/handled),CX_DYNAMIC_CHECK, CX_NO_CHECK.Catch specific exceptions first; get the text with lx->get_text( ).!Watch outNever catch cx_root and do nothing —swallowed errors become dataproblems later.4 · IN A REAL PROJECTAn interface wrapped its entire processing in TRY … CATCH cx_root … ENDTRY with an empty CATCH block "toavoid dumps". For months, conversion errors in incoming files were silently ignored, and hundreds oforders were never created. Nobody noticed until customers complained. The team replaced the empty catch with…Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Exceptions are the programmatic way ABAP code signals errors between different parts of a program — the previous lesson (Part 8) covered messages shown to users, and this one covers that lower-level mechanism. Class-based exceptions are handled with TRY ... CATCH cx_... INTO lx_ref. ...

ENDTRY, where lx_ref is declared as TYPE REF TO cx_..., for example: TRY, run the risky code; CATCH cx_sy_zerodivide INTO lx_div (with lx_div declared as TYPE REF TO cx_sy_zerodivide) to handle that specific case; CATCH cx_sy_conversion_error cx_sy_arithmetic_error INTO lx_other to handle several exception types together; and optionally a CLEANUP section that frees resources if an exception leaves the TRY block without being caught there; ENDTRY closes the construct.

Because catching a superclass also catches all of its subclasses, CATCH blocks should be ordered from specific to general.

The exception hierarchy has CX_ROOT at the top, below which sit three categories. CX_STATIC_CHECK exceptions must be declared in the method's signature with RAISING or handled, and the syntax check enforces this; they're the right choice for expected business errors that the caller must handle. CX_DYNAMIC_CHECK exceptions must also be declared, but the check only happens at runtime; this is typical of system exceptions like CX_SY_ZERODIVIDE, which callers can usually prevent. CX_NO_CHECK exceptions may occur anywhere and never need to be declared, which fits things like resource shortages.

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

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.