OO Exception-Message Class with Exception Class


 

Step1. Creae a message class in Tx- SE91 with the below message numbers and texts.


Step2. Go to Tx-SE24 and create an exception class. Provide the class name and select Create.


Step3. Provide the description and Select Radio button for Exception class and Select the Check box for the WITH MESSAGE CLASS. Save it.


Step4. As we have selected WITH MESSAGE CLASS so one interface IF_T100_MESSAGE is implemented in the exception class. Click on attributes tab.


Step5. So we have the below attributes. Now click on the TEXTs tab.


Step6. Only one Exception text ID is automatically created by the system. Now click on methods tab.


Step7. So here we have the Methods. now click on the Attributes tab.


Step8. As earlier we have the message class with message 000 which contains one place holder &1. In teh run time we will pass some value to this message 000 place holder

So create an attribute As NUM_LARGER OF TYPE I.  Now click on Texts tab.


Step9. We have the below screen.


Step10. Now provide a new Exception ID name as ‘ZNUMBER_TOO_LARGE’, then click on the button Message Text.


Step11. Here the below screen appears to map the exception text ID to the message class message number.


Step12. Provide the message class name and message number ‘000’ and hit enter key. Then click on the drop down to select already created attributes in the exception class. So in this way the attributes defined in the exception class is mapped to the  message class message text place holder.


Step13. Form the drop down select NUM_LARGE attribute and click on change.


Step14. So here the exception text Id is mapped to the message class message number.


Step15.  Now again go back to the attributes section and define two attributes DIV_NUM1 & DIV_NUM2. 


Step16. Under the Text tab define a new Exception ID as ZDIVISION_BY_ZERO and then click on Message Text button.


Step17. So from the appeared screen, select the message class name and message number. Then hit Enter button. From the attributes of Exception class, from the drop down select already defined attributes DIV_NUM1 and DIV_NUM2.


Step18. So then the Exception ID is mapped to message class text .


Step19. So here we have the program which Raises the Exception with Exception ID with ZNUMBER_TOO_LARGE. While Raising the Exception we are passing the attribute NUM_LARGE. Execute the program.


*———————————————————————-*

CLASS lcl_demo DEFINITION.
PUBLIC SECTION.
METHODS number_test IMPORTING i_num TYPE i
RAISING zcx_exception_static.
ENDCLASS.                  

*———————————————————————-*
CLASS lcl_demo IMPLEMENTATION.
METHOD number_test.
IF i_num > 100.
      RAISE EXCEPTION TYPE zcx_exception_static
EXPORTING
textid    zcx_exception_static=>znumber_too_large
*         previous  =
num_large i_num
*         div_num1  =
*         div_num2  =
.

ELSE.
WRITE :‘Given number’i_num ‘is valid’.
ENDIF.
ENDMETHOD.                  
ENDCLASS.                    

START-OF-SELECTION.

DATA num TYPE VALUE 200,
msg type string.
DATA lr_demo TYPE REF TO lcl_demo,
lr_exp TYPE REF TO zcx_exception_static.


CREATE OBJECT lr_demo.
TRY.
CALL METHOD lr_demo->number_test
EXPORTING
i_num num.
CATCH zcx_exception_static INTO lr_exp.
msg lr_exp->get_text).
WRITE/ msg.
ENDTRY.

 


 

Step20. So we have the below message which is coming from the message class with message number 000 with the correct place holder value.


Step21. Again execute the program which raises the Exception ZCX_EXCEPTION_STATIC with Exception ID : ZDIVISION_BY_ZERO and when raising we are exporting two two attributes.


*———————————————————————-*
CLASS lcl_demo DEFINITION.
PUBLIC SECTION.
METHODS division_test   IMPORTING i_num1 TYPE i
i_num2 TYPE i
RETURNING value(r_resTYPE i
RAISING zcx_exception_static.
ENDCLASS.                  

*———————————————————————-*
CLASS lcl_demo IMPLEMENTATION.
METHOD division_test.
IF i_num2 EQ 0.


      RAISE EXCEPTION TYPE zcx_exception_static
EXPORTING
textid    zcx_exception_static=>zdivision_by_zero
*         previous  =
*         num_large =
div_num1  i_num1
div_num2  i_num2.

ELSE.
r_res i_num1 / i_num2.
ENDIF.
ENDMETHOD.                   
ENDCLASS.                  

START-OF-SELECTION.

DATA num1 TYPE VALUE 20,
num2 TYPE VALUE 0,
res TYPE i,
msg TYPE string.


DATA lr_demo TYPE REF TO lcl_demo,
lr_exp TYPE REF TO zcx_exception_static.


CREATE OBJECT lr_demo.
TRY.
CALL METHOD lr_demo->division_test
EXPORTING
i_num1  num1
i_num2 num2
RECEIVING
r_res  res.
CATCH zcx_exception_static INTO lr_exp.
msg lr_exp->get_text).
WRITE/ msg.
ENDTRY.


Step22. So here we have the message text .


 Step23.


*———————————————————————-*
CLASS lcl_demo DEFINITION.
PUBLIC SECTION.
METHODS division_test IMPORTING i_num1 TYPE i
i_num2 TYPE i
RETURNING value(r_resTYPE i
RAISING zcx_exception_static.
ENDCLASS.                    “LCL_dEMO DEFINITION

*———————————————————————-*
CLASS lcl_demo IMPLEMENTATION.
METHOD division_test.
IF i_num2 EQ 0.
RAISE EXCEPTION TYPE zcx_exception_static
EXPORTING
textid    zcx_exception_static=>zdivision_by_zero
*         previous  =
*         num_large =
div_num1  i_num1
div_num2  i_num2.

ELSE.
r_res i_num1 / i_num2.
ENDIF.
ENDMETHOD.                    “NUMBER_TEST
ENDCLASS.                    “LCL_DEMO IMPLEMENTATION

START-OF-SELECTION.

DATA num1 TYPE VALUE 20,
num2 TYPE VALUE 0,
res TYPE i,
msg TYPE string.
DATA lr_demo TYPE REF TO lcl_demo,
lr_exp TYPE REF TO zcx_exception_static.
CREATE OBJECT lr_demo.
TRY.
CALL METHOD lr_demo->division_test
EXPORTING
i_num1  num1
i_num2 num2
RECEIVING
r_res  res.
CATCH zcx_exception_static INTO lr_exp.

   MESSAGE lr_exp type ‘I’.
ENDTRY.


Step24.


Step25.


*———————————————————————-*
CLASS lcl_demo DEFINITION.
PUBLIC SECTION.
METHODS division_test IMPORTING i_num1 TYPE i
i_num2 TYPE i
RETURNING value(r_resTYPE i
RAISING zcx_exception_static.
ENDCLASS.                    “LCL_dEMO DEFINITION

*———————————————————————-*
CLASS lcl_demo IMPLEMENTATION.
METHOD division_test.
IF i_num2 EQ 0.
RAISE EXCEPTION TYPE zcx_exception_static
EXPORTING
textid    zcx_exception_static=>zdivision_by_zero
*         previous  =
*         num_large =
div_num1  i_num1
div_num2  i_num2.

ELSE.
r_res i_num1 / i_num2.
ENDIF.
ENDMETHOD.                    “NUMBER_TEST
ENDCLASS.                    “LCL_DEMO IMPLEMENTATION

START-OF-SELECTION.

DATA num1 TYPE VALUE 20,
num2 TYPE VALUE 0,
res TYPE i,
msg TYPE string.
DATA lr_demo TYPE REF TO lcl_demo,
lr_exp TYPE REF TO zcx_exception_static.
CREATE OBJECT lr_demo.
TRY.
CALL METHOD lr_demo->division_test
EXPORTING
i_num1  num1
i_num2 num2
RECEIVING
r_res  res.
CATCH zcx_exception_static INTO lr_exp.

     MESSAGE ID lr_exp->if_t100_message~t100keymsgid
TYPE ‘I’              NUMBER lr_exp->if_t100_message~t100keymsgno
WITH num1 num2.

  ENDTRY.


Step26. 


 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s