Catch the Dump in ABAP – II

0
706

As an ABAPer, you probably know how combursome it is to work with classical exceptions (discussed in last post), since you must handle all of them immediately after a function or method call. Beside being labourious, these mappings clutter our code and make it difficult to read.

 SAP also have Object Oriented Class based exceptions which are easy to handle and less cumbersome.

 

Class-based exceptions are handled in the following control structure:

TRY.

” TRY block (application coding)

CATCH cx_… cx_… …

” CATCH block (exception handler)

CATCH cx_… cx_… …

” CATCH block (exception handler)

CLEANUP.

” CLEANUP block (cleanup context)

ENDTRY.

 

The TRY statement opens a control structure to be ended with ENDTRY, in which three statement blocks can be listed in a specified order (this is not obligatory).

 1. A TRY block, in which exceptions can occur.

This exception block consists of all the statements between the TRY and the CATCH statement.

 2. One or more CATCH blocks for catching exceptions.

These exception blocks are initiated with CATCH and ended with a further CATCH, CLEANUP, or ENDTRY.

 3. A CLEANUP block for cleanup work after the exceptions have been caught.

This statement block is initiated by CLEANUP and ended with ENDTRY. A TRY-ENDTRY structure must not contain more than one CLEANUP block in precisely this position.

 We can also raise explicit exception by using statement RAISE EXCEPTION <CUSTOM_EXCEPTION>.

  

Sample Report

 Report DEMO_HANDLE_EXCEPTIONS.

parameters NUMBER type I.

data RESULT type P decimals 2.
data OREF type ref to CX_ROOT.
data TEXT type STRING.

start-of-selection.
write: / ‘Testing division and Sqare root with’, NUMBER.

uline.

try.

if ABS( NUMBER ) > 100.
raise exception type CX_DEMO_ABS_TOO_LARGE.

endif.

try.

RESULT = 1 / NUMBER.
write: / ‘Result of division:’, RESULT.
RESULT = SQRT( NUMBER ).
write: / ‘Result of square root:’, RESULT.

catch CX_SY_ZERODIVIDE into OREF.
TEXT = OREF->GET_TEXT( ).
cleanup.
clear RESULT.

endtry.

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.
write / TEXT.
endif.

write: / ‘Final result:’, RESULT.

 

The output of the above program when zero (0) is entered:

 

Catching Exception
Catching Zero (0) Exception

 

 

  

LEAVE A REPLY

Please enter your comment!
Please enter your name here