Treat invalid input as an explicit outcome
A parcel cannot be tracked without a tracking code. Returning a normal-looking summary for invalid input hides the problem. A class-based exception gives the method a way to report that it could not fulfill its contract.
Declare and raise a specific exception
CLASS lcx_missing_code DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.
METHODS validate
IMPORTING iv_code TYPE string
RAISING lcx_missing_code.
METHOD validate.
IF iv_code IS INITIAL.
RAISE EXCEPTION TYPE lcx_missing_code.
ENDIF.
ENDMETHOD.
cx_static_check is a checked-exception category. The method advertises the exception in RAISING, so callers must handle or propagate it according to the language rules. Other exception categories exist; choose based on whether callers should be required to handle the condition.
Catch at a useful boundary
TRY.
validator->validate( iv_code = input_code ).
CATCH lcx_missing_code.
result = 'Tracking code is required'.
ENDTRY.
Catch the most specific exception you can handle. Do not catch everything merely to continue with invalid state; log, report, or recover in a way that makes the failure visible.
Practice
Create a checked exception type, raise it for an initial code, and handle that type in CATCH. Assign a clear message to result; then add environment output. This is structural practice, not a running exception test.
Version note: Exception class availability and allowed APIs are release/language-version dependent. The example uses a local exception class to avoid external dependencies.
Sources
- SAP Samples, ABAP Object Orientation: class-based exceptions
- SAP Help Portal, ABAP Objects overview
Developer practice · ABAP
Practice: reject a missing tracking code
Declare a checked exception, raise it when the tracking code is initial, and handle it with a specific CATCH.
This choice changes the output pattern checked here: WRITE belongs in a classic executable report, while out->write( ) uses the supplied output reference in an ABAP Cloud class-run method. It does not switch the language version of the other code; check release and API availability in your target system.
Complete the task and include the output statement shown for your selected environment. Variable names can differ if you use them consistently. This browser checker checks structure only; it does not compile or run ABAP.
0 required structures detected · 0 checks
Write your solution, then check its structure.
One possible solution
CLASS lcx_missing_code DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.
CLASS lcl_validator DEFINITION.
PUBLIC SECTION.
METHODS validate IMPORTING iv_code TYPE string RAISING lcx_missing_code.
ENDCLASS.
CLASS lcl_validator IMPLEMENTATION.
METHOD validate.
IF iv_code IS INITIAL.
RAISE EXCEPTION TYPE lcx_missing_code.
ENDIF.
ENDMETHOD.
ENDCLASS.
DATA(validator) = NEW lcl_validator( ).
TRY.
validator->validate( iv_code = '' ).
DATA(result) = 'Code accepted'.
CATCH lcx_missing_code.
result = 'Tracking code is required'.
ENDTRY.
WRITE result.out->write( result ).These are context-specific output examples. In your own program, use the result variable from your solution and the output mechanism available in that program.