ABAP Keyword Documentation → ABAP − Reference → Program Flow Logic → Exception Handling → Class-Based Exceptions → Examples of Exceptions
Exceptions, TRY
This example demonstrates the TRY
control structure.
Other versions: 7.31 | 7.40 | 7.54
Source Code
REPORT demo_try.
CLASS try_demo DEFINITION.
PUBLIC SECTION.
CLASS-DATA: result TYPE p LENGTH 8 DECIMALS 2,
oref TYPE REF TO cx_root,
text TYPE string.
CLASS-METHODS main.
PRIVATE SECTION.
CLASS-DATA number TYPE i.
CLASS-DATA out TYPE REF TO if_demo_output.
CLASS-METHODS calculation
IMPORTING p_number LIKE number
CHANGING p_result LIKE result
p_text LIKE text
RAISING cx_sy_arithmetic_error.
ENDCLASS.
CLASS try_demo IMPLEMENTATION.
METHOD main.
cl_demo_input=>request( CHANGING field = number ).
out = cl_demo_output=>new( ).
TRY.
IF abs( number ) > 100.
RAISE EXCEPTION TYPE cx_demo_abs_too_large.
ENDIF.
calculation( EXPORTING p_number = number
CHANGING p_result = result
p_text = text ).
CATCH cx_sy_arithmetic_error INTO oref.
text = oref->get_text( ).
CATCH cx_root INTO oref.
text = oref->get_text( ).
ENDTRY.
IF NOT text IS INITIAL.
out->write( text ).
ENDIF.
out->display( |Final result: { result ALIGN = LEFT }| ).
ENDMETHOD.
METHOD calculation.
DATA l_oref TYPE REF TO cx_root.
TRY.
p_result = 1 / p_number.
out->write(
|Result of division: { p_result ALIGN = LEFT }| ).
p_result = sqrt( p_number ).
out->write(
|Result of square root: { p_result ALIGN = LEFT }| ).
CATCH cx_sy_zerodivide INTO l_oref.
p_text = l_oref->get_text( ).
CLEANUP.
CLEAR p_result.
ENDTRY.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
try_demo=>main( ).
Description
- If the content of
number
is greater than 100, theTRY
control structure of the master program raises an exception of the class CX_DEMO_ABS_TOO_LARGE in theTRY
block. This exception is handled by the secondCATCH
block of the sameTRY
control structure, since the subclass of the most general exception is CX_ROOT.
- If the content of
number
is 0, the runtime environment in theTRY
block of theTRY
control structure of the called methodcalculation
raises an exception of the predefined class CX_SY_ZERODIVIDE. This is handled in theCATCH
block of the sameTRY
control structure.
- If the content of
number
is a negative number, the runtime environment in theTRY
block of theTRY
control structure of the called methodcalculation
raises an exception of the predefined class CX_SY_ARG_OUT_OF_DOMAIN. Since there is no handler defined for this exception in thisTRY
control structure, the exception is propagated from the method. This is made possible by the declaration of the superclass CX_SY_ARITHMETIC_ERROR usingRAISING
in the method interface. TheCLEANUP
block of the innerTRY
control structure is executed first.
- Any other exceptions are handled in the final
CATCH
block of the TRY control structure of the master program. This block catches all possible exceptions by specifying the most general exception class, CX_ROOT. If, for example, CX_SY_ARG_OUT_OF_DOMAIN or one of its superclasses is not declared in the method interface, the exception CX_SY_NO_HANDLER would be raised and would be handled in the finalCATCH
block.