Code examples · Modern ABAP · Beginner
ABAP class-based exceptions: raise your own exception and catch it
The problem
A booking method must refuse a booking with an amount of zero or less.
How we solve it
- 1Exception classZCX_BOOKING_ERROR
- 2Raise itRAISE EXCEPTION
- 3Catch itTRY ... CATCH
The result
A booking with a negative amount is refused with a clear reason, a division by zero is reported separately, and a valid booking succeeds.
SAP objects in play
1. The requirement
A booking method must refuse a booking with an amount of zero or less. The caller must be able to tell this business error apart from a technical error such as a division by zero, and must show the reason to the user.
2. How we design it
A business rule that is broken is reported with an exception, not with a return code that the caller may forget to check. We write our own exception class that carries the reason as text. The method declares it in RAISING, so the compiler forces every caller to deal with it. The caller catches the most specific exception first and the general one last.
Why we decided it this way
- 1Inherit from CX_STATIC_CHECKThe compiler warns if a caller neither catches the exception nor passes it on, so it cannot be forgotten.
- 2Carry the reason as an attributeThe caller can show or log the exact reason without parsing a message text.
- 3Most specific CATCH firstABAP uses the first CATCH that fits. A general CATCH placed first would hide the specific ones.
Before you start
- ADT (ABAP Development Tools) and a package such as $TMP or your own Z package.
3. The code, step by step
Step 1 of 3: Create the exception class
An exception that remembers why it was raised.
" Definition
CLASS zcx_booking_error DEFINITION PUBLIC
INHERITING FROM cx_static_check
FINAL CREATE PUBLIC.
PUBLIC SECTION.
DATA reason TYPE string READ-ONLY.
METHODS constructor
IMPORTING reason TYPE string OPTIONAL
previous LIKE previous OPTIONAL.
ENDCLASS.
" Implementation
CLASS zcx_booking_error IMPLEMENTATION.
METHOD constructor.
super->constructor( previous = previous ).
me->reason = reason.
ENDMETHOD.
ENDCLASS.
Part by part (plain text)
- Definition. The class inherits from CX_STATIC_CHECK, the base for exceptions the compiler wants handled. The attribute REASON is read-only: it is set once, in the constructor. The constructor also accepts PREVIOUS, so you can chain an earlier exception.
- Implementation. The constructor first passes PREVIOUS to the parent class, then stores the reason in the attribute.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
ABAP dumps
4. The final outcome
A booking with a negative amount is refused with a clear reason, a division by zero is reported separately, and a valid booking succeeds.
What you should see
Booked 100.00 Business error: Amount must be positive, got -5.00 Technical error: division by zero (CX_SY_ZERODIVIDE)
Try it and check the result
- Activate both classes and run ZCL_BOOKING_DEMO with F9. The console shows the three lines of the outcome.
- Remove the CATCH for ZCX_BOOKING_ERROR and activate: the syntax check warns that the exception is neither caught nor declared. This is the compiler protecting you.
- Move CATCH cx_root above CATCH cx_sy_zerodivide and activate: the syntax check reports the specific catch can never be reached.
Common mistakes
- Never write an empty CATCH block. A swallowed error is the hardest bug to find later.
- Do not use CX_ROOT as the only handler. You lose the difference between a business error and a technical one.
- An exception is for the exceptional case. Do not use it for normal control flow such as 'not found' in a loop; return an empty result there.