Thursday 26 June 2014

Using Exception Class with Message Class In OOABAP

Step1. Go to Tx- SE91 and Create a message.













Step2. Go to Tx- Se24 and create an Exception class.











Step3. Select the highlighted parts and Click on Save button.

















Step4. Activate the class. Go to Attributes section.












Step5. Define as attribute : I_VBELN.Then go to Texts tab.














Step6. Under the Exception ID Provide a name such as 'SALES_ORDER_NOT_FOUND' and then click on the Message Text button.













Step7. Provide the Message class created above, message number and from the attributes section, from the drop down select previously created attribute 'I_VBELN'. That means when this Exception ID is raised it uses the attribute I_VBELN and pass it to the message class number.

















Step8. Activate the Exception class.













-------------------------------------------------------------------------------------------------------------------------------
Step9. Create the below program which throws the exception of the above created class.

REPORT  ztest_exception_with_msg.

CLASS lcl_salesord DEFINITION.
  PUBLIC SECTION.
    DATA : vbeln TYPE vbak-vbeln,
                  ls_vbak TYPE vbak.
    METHODS : constructor IMPORTING i_vbeln TYPE vbak-vbeln,
                          get_salesorder RAISING zcx_exception_test,
                          show_salesorder.
ENDCLASS.                    "lcl_salesord DEFINITION

CLASS lcl_salesord IMPLEMENTATION.
  METHOD constructor.
    vbeln = i_vbeln.
  ENDMETHOD.                    "constructor

  METHOD  get_salesorder.
    SELECT SINGLE * FROM vbak INTO ls_vbak WHERE vbeln = vbeln.
    IF sy-subrc <> 0.
      RAISE EXCEPTION TYPE zcx_exception_test
        EXPORTING
          textid = zcx_exception_test=>sales_order_not_found
          i_vbeln  = vbeln.
    ENDIF.
  ENDMETHOD.                    "get_salesorder

  METHOD show_salesorder.
    WRITE :/ ls_vbak-vbeln,
             ls_vbak-erdat,
             ls_vbak-ernam,
             ls_vbak-vbtyp,
             ls_vbak-erdat,
             ls_vbak-vkorg,
             ls_vbak-vtweg,
             ls_vbak-spart.

  ENDMETHOD.                    "show_salesorder
ENDCLASS.                    "lcl_salesord IMPLEMENTATION

START-OF-SELECTION.

  PARAMETERS : p_vbeln TYPE vbak-vbeln.
  DATA : obj_x TYPE REF TO zcx_exception_test.
  DATA: obj1 TYPE REF TO lcl_salesord.
  DATA : result TYPE string.

  CREATE OBJECT obj1
    EXPORTING
      i_vbeln = p_vbeln.
  TRY.
      CALL METHOD obj1->get_salesorder.
      CALL METHOD obj1->show_salesorder.
    CATCH zcx_exception_test INTO obj_x.
      CALL METHOD obj_x->get_text
        RECEIVING
          result = result.
      WRITE :/ result.
  ENDTRY.

------------------------------------------------------------------------------------------------------------------------------
Step10. Execute the program.




















Step11. Provide a correct sales order number.










Step12. The program executed without any exception.












Step13. Again run the program and provide an incorrect sales order number.










Step14. The exception is raised and the below message is displayed.














-------------------------------------------------------xxxxx-----------------------------------------------------------------

No comments:

Comments system

Disqus Shortname