Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Chaining OOPS exception objects - able to access previous variables?

Former Member
0 Likes
3,056

Hi all,

I'm writing a program and passing some of my exceptions up without handling in the immediate method. I'm doing this by CATCHing the exception in the method, and then RAISING a new exception of another type. I'm wanting to access some parameters that I'm assigning to the object that will be passed in the 'previous' parameter of the RAISING, but I'm getting errors saying that the object doesn't have those parameters. Upon further review, I see that the 'previous' parameter is TYPE REF TO cx_root. So my question is, is it possible to pass these parameters up without assigning them to the new exception?

Relevant code (this is inherited from the zcx_excel exception from ABAP2XLSX):

Definition/Implementation:

CLASS lcx_excel DEFINITION INHERITING FROM zcx_excel FINAL CREATE PUBLIC.

   PUBLIC SECTION.

     DATA: attr1 TYPE string,    "specific error name (workbook, worksheet, etc.)

           attr2 TYPE string,    "constant for error type

           attr3 TYPE string.    "sy-subrc, if applicable

     METHODS constructor

               IMPORTING

                 !textid   LIKE textid   OPTIONAL

                 !previous LIKE previous OPTIONAL

                 !error    TYPE string   OPTIONAL

                 !attr1    TYPE string   OPTIONAL

                 !attr2    TYPE string   OPTIONAL

                 !attr3    TYPE string   OPTIONAL.

ENDCLASS.

CLASS lcx_excel IMPLEMENTATION.

   METHOD constructor.

     CALL METHOD super->constructor

       EXPORTING

         error    = error

         textid   = textid

         previous = previous.

     IF textid IS INITIAL.

       me->textid = zcx_excel.

     ENDIF.

     me->error = error.

     me->attr1 = attr1.

     me->attr2 = attr2.

     me->attr3 = attr3.

   ENDMETHOD.

ENDCLASS.


code as in program:

RAISE EXCEPTION TYPE lcx_excel

         EXPORTING

           error = 'test error'

           attr1 = 'attr1'

           attr2 = 'attr2'.


......................

CATCH zcx_excel INTO lo_zcx_excel.        "map exception to local class lcx_excel

       lv_errormessage = 'Error during READ_EXCEL'.

       RAISE EXCEPTION TYPE lcx_excel          "raise local exception class lcx_excel

         EXPORTING

           error    = lv_errormessage

           attr1    = lv_workbook_title    "workbook in error

           attr2    = lv_worksheet_name    "worksheet in error

           previous = lo_zcx_excel.


->go up to calling form:


CATCH  lcx_excel INTO lo_lcx_excel.

       DATAlv_previouserror TYPE string.

        lv_errormessage  = lo_lcx_excel->previous->error.     <- this is where I am not able to find the error parameter. How would I access these parameters?


I'd like to access the error, attr1, attr2, attr3 parameters of the previous reference without passing them to the RAISE lcx_excel?


1 ACCEPTED SOLUTION
Read only

naimesh_patel
Active Contributor
0 Likes
1,358

You need to pass the additional information as part of the "Text". In the Text, you can embed the parameters, which you have created.

When you catch the exception, call the PREVIOUS->GET_TEXT( ) to get the text. This method is implemented in the CX_ROOT but will be called using the reference of the exception PREVIOUS. The method actually replaces the current values in the place holders which may exist in the exception Text.

Otherwise, if you know what could be the PREVIOUS, you can declare a variable with reference to that exception class, cast PREVIOUS to that variable and call the required attributes. But, this limits the flexibility.

Regards,
Naimesh Patel

2 REPLIES 2
Read only

naimesh_patel
Active Contributor
0 Likes
1,359

You need to pass the additional information as part of the "Text". In the Text, you can embed the parameters, which you have created.

When you catch the exception, call the PREVIOUS->GET_TEXT( ) to get the text. This method is implemented in the CX_ROOT but will be called using the reference of the exception PREVIOUS. The method actually replaces the current values in the place holders which may exist in the exception Text.

Otherwise, if you know what could be the PREVIOUS, you can declare a variable with reference to that exception class, cast PREVIOUS to that variable and call the required attributes. But, this limits the flexibility.

Regards,
Naimesh Patel

Read only

0 Likes
1,358

Hi Naimesh,

Delcare a variable with reference to the exact type (if known in advance) of the previous exception class; then set that to the previous error instance with lo_prev_err ?= lo_lcx_excel->previous.  The attributes of that exception class are public so you can access them as lo_prev_err->(attribute_name).

Jim