Exception Handling - Classical vs Class-Based
Searchable keywords: exception, RAISING, TRY, CATCH, cx_static_check, cx_dynamic_check, EXCEPTIONS, sy-subrc, class-based exception, classical exception
TOPICS IN THIS FILE
- Quick Identification - line 5
- Classical Exceptions - line 12
- Class-Based Exceptions - line 25
- Method Signatures - line 50
- Best Practices - line 80
- Interface vs Class Methods - line 118
ABAP has two exception handling mechanisms. Using the wrong one causes silent failures.
Quick Identification
| Method Signature | Exception Type | Handling Pattern |
|---|---|---|
EXCEPTIONS exc = 1 |
Classical | sy-subrc check |
RAISING cx_... |
Class-based | TRY-CATCH |
Classical Exceptions (Old Style)
Methods declared with EXCEPTIONS in signature:
" Method declaration
class-methods describe_by_name
importing p_name type any
returning value(p_descr_ref) type ref to cl_abap_typedescr
exceptions
type_not_found. " <- Classical!
" Calling with classical exception
cl_abap_structdescr=>describe_by_name(
EXPORTING
p_name = iv_tabname
RECEIVING
p_descr_ref = lo_descr
EXCEPTIONS
type_not_found = 1
OTHERS = 2 ).
IF sy-subrc <> 0.
" Handle error
ENDIF.
Common classical exception methods:
cl_abap_structdescr=>describe_by_name-type_not_foundcl_abap_tabledescr=>describe_by_name-type_not_found- Many RTTI (Run Time Type Information) methods
Class-Based Exceptions (Modern)
Methods declared with RAISING:
" Method declaration
methods get_ref
importing p_name type string
returning value(p_ref) type ref to cl_ci_checkvariant
raising cx_ci_checkvariant. " <- Class-based!
" Calling with TRY-CATCH
TRY.
lo_variant = cl_ci_checkvariant=>get_ref( p_name = lv_variant ).
CATCH cx_ci_checkvariant INTO DATA(lx_error).
" Handle error
ENDTRY.
The Mistake to Avoid
WRONG: Using TRY-CATCH for classical exceptions
" This does NOT catch type_not_found!
TRY.
lo_descr = cl_abap_structdescr=>describe_by_name( iv_name ).
CATCH cx_root. " Never reached!
ENDTRY.
CORRECT: Use proper pattern for each type
" Classical - use sy-subrc
cl_abap_structdescr=>describe_by_name(
EXPORTING p_name = iv_name
RECEIVING p_descr_ref = lo_descr
EXCEPTIONS type_not_found = 1 ).
IF sy-subrc <> 0.
" Handle error
ENDIF.
" Class-based - use TRY-CATCH
TRY.
lo_obj = cl_ci_checkvariant=>get_ref( lv_name ).
CATCH cx_root INTO DATA(lx_error).
" Handle error
ENDTRY.
How to Check Which to Use
Use the view command to check method signature:
abapgit-agent view --objects CL_ABAP_STRUCTDESCR
Look for:
exceptions TYPE_NOT_FOUND→ Classical (usesy-subrc)raising CX_SY_RTTI→ Class-based (useTRY-CATCH)
Or search the cheat sheets:
abapgit-agent ref --topic exceptions
Interface vs Class Methods
Interface Methods
Cannot add RAISING clause in the implementing class. Options:
- Add RAISING to interface definition
- Use TRY-CATCH in the implementation
" Interface definition - can add RAISING here
INTERFACE zif_example.
methods execute
importing is_param type data optional
returning value(rv_result) type string
raising cx_static_check.
ENDINTERFACE.
" Implementation - CANNOT add RAISING here
CLASS zcl_example DEFINITION.
INTERFACES zif_example.
ENDCLASS.
CLASS zcl_example IMPLEMENTATION.
METHOD zif_example~execute.
" Must handle cx_static_check here with TRY-CATCH
" or declare it in interface, not here
ENDMETHOD.
ENDCLASS.
Class Methods
Can add RAISING clause to declare exceptions, allowing caller to handle in one place:
" Class method with RAISING clause
METHODS process_data
importing iv_data type string
returning value(rv_result) type string
raising cx_static_check.
" Caller can handle in one place
TRY.
lv_result = lo_obj->process_data( iv_data = 'test' ).
CATCH cx_static_check.
" Handle exception
ENDTRY.
When to Use Each
| Scenario | Recommendation |
|---|---|
| Multiple callers need to handle exception | Add RAISING to method definition |
| Exception should be handled internally | Use TRY-CATCH in implementation |
| Interface method | Add RAISING to interface, or use TRY-CATCH in class |
RAISING Clause in Method Calls
The RAISING clause cannot be used in method call statements. It can only be used in method definitions.
" WRONG - syntax error
lo_handler->read( ... ) RAISING cx_dd_ddl_check.
" CORRECT - use TRY-CATCH
TRY.
lo_handler->read( ... ).
CATCH cx_dd_ddl_check.
" Handle error
ENDTRY.
Correct usage in method definitions:
abap
" Interface/Class method definition
METHODS read
IMPORTING iv_name TYPE string
RAISING cx_static_check.
|