Google Search - Blog...........

ABAP - OOPS: Objects Created From Exception Classes When Error Is Trapped.

When a class-based exception is trapped using TRY…CATCH…ENDTRY statement, objects are created from the exception class. One can create the object using CATCH INTO statement.

CX_ROOT is at the top of the inheritance tree for all SAP provided exception class and have some pre-defined methods available, which are adopted by all exception-classes.

-------------------------------------------------------------------------------------------
REPORT YSUBOOPS17 .
data : inum type i value 5 ,
descrip type string ,
progname like sy-repid ,
lineno type i .

data : eref type ref to cx_sy_zerodivide.

try.
inum = inum / 0.
CATCH cx_sy_zerodivide into eref.
* Utilizing methods/attributes using object of the exception classes
call method eref->get_text
receiving result = descrip.
write:/5 'Name of the error trapped : ' , descrip.

call method eref->get_source_position
importing program_name = progname
source_line = lineno .
write:/5 'Error detected in program' ,
progname(15) ,
'line number' ,
lineno.
write:/5 eref->kernel_errid.
endtry.


Output:

Name of the error trapped : Division by zero
Error detected in program YSUBDEL line number 10
COMPUTE_INT_ZERODIVIDE

No comments:

Post a Comment