Gyan Factory

Gyan Factory
SAP Technical Project Support

Friday, January 8, 2016

OO ABAP Exception: Using Global Exception class with TEXTs

Step1. Go o Tx- SE24 and provide an Exception class name and select Create button.

Step2. Super class here is CX_STATIC_CHECK and select the Exception class radio button and Save.


Step3. So here are teh below methods inherited form the super class. Then slick on TEXT tab.

Step4. So here we have two  Exception IDs are available. New text ids can be created. All text IDs of the exception class are part of the attributes. Now select the attributes tab.

Step5. Here the two Text IDs are part of the exception class attribute and also some more predefined attributes are there.

Step6. In Tx- SE24 create a global class which will use the above created Exception in its method signature & in method implementation.

Step7. Select below points & Save.

Step8. Under the methods tab, define two method -> RUN and VALIDATE NUMBER with below level and visibility. Mouse click on the VALIDATE_NUMBER method & click on Parameter button to define its signature.

Step9. Now for the VALIDATE_NUMBER method provide the below shown parameters and click on Exceptions button to define some exception for the method VALIDATE_NUMBER.

Step10. Provide the above created Exception class name and then finally click on the methods button.

Step11. Similarly select the RUN method and click on parameters button to define method signature.Provide the below parameters as shown and then to define Exception for this method click on the Exceptions button.

Step12. Provide the above created Exception class name below and then click on Methods tab.


Step13. Then Double click on the RUN method to implement it and inside this call the VALIDATE_NUMBER method. Go back and then implement the VALIDATE_NUMBER method.

Step14. So we have the below code in VALIDATE_NUMBER method, If the provided number is grater than 100 then its raised the declared exception which is not handled her but handled in the RUN method which calls this VALIDATE_NUMBER method.

Step15. So here we have the report program which calls the RUN method of the above created class.

Step16. Run the report with a number less than 100 and there is no exception is raised and we have the below output.







  



               




                                                                                                                            
 Step17. Go to Tx- SE24 & edit our exception class. Under the TEXT tab, for the 
 Exception ID ZCX_STATTIC_EXCEPTION , maintain the Text as s' Number is too large' and activate the class.

Step18. As we know that each exception ID under the text tab is a attribute of the class.

Step19. Make some changes in the VALIDATE_NUMBER method. So when the exception is raised it exports the exception class attribute ZCX_STATIC_EXCEPTION.(here the exception class name and one of the attribute name is same). When the run method catches the raised exception occurred in the VALIDATE_NUMBER method , it calls the get_text method and display the exception message.

Step20. Runs the report and we have the below output.





























Step21. Now go to Tx- SE24 and Under the TETX tab of the exception class, Select the Exception ID 'ZCX_STATIC_EXCEPTION' and click on the Long text button.

Step22. Maintain the long text which is specific for this exception ad save and finally continue.

Step23. So in the RUN method, After caching the raised exception , call the get_longtext() method. Execute observe the output.

Step24. In the above case the Exception is raised in the VALIDATE_NUMBER method and it is handled/catched inside the RUN method and the exception text/long text is shown there but the calling program is not informed what has happened inside. Lets navigate the raised exception from the point of raise to the point of the call.
Report calls RUN method-> RUN method calls VALIDATE_NUMBER method->Exception is raised inside VALIDATE_NUMBER method. 
So when Exception is raised inside the VALIDATE_NUMBER method, it is catched in RUN method, When it is catched in RUN method it again Raises the same exception which is catched in the report. Make significant changes in the report and methods as shown below. Execute the report and we have the output.


Step25. Go to the exception class and define a new exception id under the TEXT tab. 


Step26. So we have a new exception ID as 'ZNUMBER_NOT_VALID' with text. Go to the attributes tab.

Step27. So the create new exception id under the TEXT tab is available in the attributes tab.

Step28. In this case the VALIDATE_NUMBER METHOD raises the exception with ID as 'ZCX_STATIC_EXCEPTION' and then it is handled /cathced in the RUN method and the text is displayed. Again in RUN method it raises the same exception with a different text id as 'ZNUMBER_NOT_VALID' which is handled in the calling report and text is displayed.

Step29. Run the report and here we have the output.


   
















Step30. In above case, we displayed the two different  texts, one  in the RUN method and other in the report . Now how we can get all the text in one place.

When the exception is raised inside the VALIDATE_NUMBER method with a particular text id, it is handled in side the RUN method. So here again it raises the exception with a different text ID and previous as the exception object inside the RUN method. 
The Exception raised in the RUN method is catched inside the report and it calls the TEXTs as mentioned below. 


Step31. Execute the report ans we have the output.













No comments:

Post a Comment