Exceptions — designing the failure, not just catching it¶
Level: 201 · working knowledge
Status: stub — the design questions are here; the worked example is not yet.
One line: ABAP has three error mechanisms in active use — class-based exceptions, classic function-module exceptions, and MESSAGE types that end a step — and most bad error handling in a system comes from translating between them badly rather than from any one of them.
The three, and where each comes from¶
| Mechanism | Raised by | Reported as |
|---|---|---|
Class-based (cx_…) |
RAISE EXCEPTION, modern APIs |
an object, caught with TRY … CATCH |
| Classic | function modules | a number you assign, read from sy-subrc |
MESSAGE |
anything, anywhere | a screen message that may also end the step |
Code that calls a BAPI, wraps it in a class, and is itself called from an OData handler has to move an error across all three. Each translation is a chance to lose the details — which document, which row, which value — and the usual symptom is a log full of "Error occurred".
The design questions¶
- Which flavour?
CX_STATIC_CHECKforces every caller to deal with it;CX_DYNAMIC_CHECKdoes not;CX_NO_CHECKpropagates to the top. The choice is about your API's contract, not about the failure's severity. SeeTRY,CATCH,RAISE. - What does the exception carry? Typed attributes — the document number, the amount, the row index — can be tested and displayed. A pre-formatted message string can only be printed.
- Wrap or propagate?
PREVIOUSchains an exception to its cause, so a wrapper can add context without discarding the original. ACATCHthat raises a new exception and dropsPREVIOUSthrows away the stack trace. - Where is the boundary? Exactly one layer should turn an exception into something a user sees. Everywhere else it should keep travelling.
What this page still needs¶
- [ ] a custom exception class with
PREVIOUS, a text from a message class, andget_text( )worked through - [ ]
RESUMABLEexceptions andRETRY/RESUME, which almost nobody uses and occasionally should - [ ] a BAPI
RETURNtable converted into a class-based exception, as a reusable pattern - [ ] what a short dump in
ST22shows, and how to read it back to the raising line
See also¶
TRY,CATCH,RAISE— the syntax, with a programMESSAGE— the mechanism that ends a step instead of raisingCALL FUNCTION— classic exceptions andsy-subrcASSERT— for what should be impossible- BAPIs and RFC —
BAPIRET2, the most common error shape in SAP RETRYandRESUME— going back after an exceptionRAISE SHORTDUMP— a dump on purpose, carrying the facts- Short dumps — reading
ST22