Skip to main content

Module 10: Messages & Exception Handling

Handling errors correctly is critical for robust, maintainable, and user-friendly ABAP applications.
Modern ABAP strongly favors class-based exceptions over procedural error handling.


1. Message Classes​

What is a Message Class?​

A message class is a container for storing reusable messages, maintained using transaction SE91.

Message Structure​

  • Message ID (class)
  • Message number
  • Message text (with placeholders)

Example message:

Message Class: ZMSG
Number: 001
Text: Invalid input value &1
Why Message Classes Matter

Message classes centralize texts, support translations, and ensure consistent messaging across applications.


2. MESSAGE Statement​

The MESSAGE statement is used to display messages to users.


2.1 MESSAGE Types​

TypeMeaningBehavior
SSuccessContinues processing
IInformationPopup, continues
WWarningContinues
EErrorStops processing
AAbortTerminates program
XExitRuntime termination
MESSAGE s001(zmsg) WITH p_value.
MESSAGE e002(zmsg).

:::warning[Legacy Nature]
Direct use of MESSAGE statements inside business logic is not recommended in modern ABAP.
:::

## 3. Modern Message Handling Strategy (7.5+)

:::tip[Recommended Approach]

- Use MESSAGE only at UI / report layer

- Use exceptions for business logic

- Separate error detection from error display
:::

## 4. TRY…CATCH Block

The TRY…CATCH block handles exceptions in ABAP.
```abap
TRY.
" Risky operation
CATCH cx_sy_conversion_error INTO DATA(lo_exc).
MESSAGE lo_exc->get_text( ) TYPE 'E'.
ENDTRY.
Why TRY…CATCH Matters

It prevents program termination and allows controlled error handling.

Class-based exceptions are the modern and preferred way to handle errors.

5.1 Creating an Exception Class​

Exception classes usually inherit from:

  • CX_STATIC_CHECK (checked exceptions)

  • CX_DYNAMIC_CHECK (runtime exceptions)

  • CX_NO_CHECK (unchecked exceptions)

CLASS zcx_invalid_input DEFINITION
INHERITING FROM cx_static_check.
PUBLIC SECTION.
METHODS constructor
IMPORTING iv_text TYPE string.
ENDCLASS.

CLASS zcx_invalid_input IMPLEMENTATION.
METHOD constructor.
super->constructor( ).
me->previous = previous.
me->textid = iv_text.
ENDMETHOD.
ENDCLASS.

5.2 Raising an Exception​

RAISE EXCEPTION NEW zcx_invalid_input(
iv_text = 'Invalid quantity entered'
).
Modern Syntax

Always use RAISE EXCEPTION NEW (7.4+) instead of older syntax.

5.3 Catching a Custom Exception​

TRY.
lo_service->process( iv_qty = lv_qty ).
CATCH zcx_invalid_input INTO DATA(lo_exc).
MESSAGE lo_exc->get_text( ) TYPE 'E'.
ENDTRY.

6. MESSAGE vs EXCEPTION (Clear Rule)​

AspectMESSAGEException
PurposeUser interactionBusiness logic
LayerUI / ReportService / Domain
TestabilityPoorExcellent
Clean ABAP❌✅
Golden Rule

Never mix MESSAGE statements with business logic.

7. Common Mistakes​

Avoid These
  • Using MESSAGE inside methods

  • Catching CX_ROOT blindly

  • Ignoring exception propagation

  • Using RETURN codes instead of exceptions

  • Showing technical messages to users

8. Summary​

  • Message classes store reusable texts

  • MESSAGE types control program flow

  • TRY…CATCH enables controlled error handling

  • Class-based exceptions are the modern standard

  • Separate UI errors from business logic

9. Practice Exercises​

  • Create a message class and use placeholders.

  • Replace MESSAGE-based validation with exceptions.

  • Create and raise a custom exception.

  • Catch and display exception messages cleanly.

  • Refactor legacy error handling to class-based.

10. What’s Next?​

➡️ Module 11: ABAP Objects (OOP – Core)

Learning Tip

Mastering exceptions is a hallmark of a senior ABAP developer.