ABAP Objects: Custom SAP ERP HCM Class Library – Example 3 – Exceptions
This blog focuses on ABAP Objects exceptions handling and will provide coding examples from the custom SAP ERP HCM class library. I will introduce an easy to use, maintainable, and flexible exception handling that can be generally applied in ABAP Objects within all functional areas. If you are not familiar with ABAP Objects exception handling, please read Gerd Kluger and Christoph Wedler’s article A Programmer’s Guide to the New Exception – Handling Concept in ABAP for a more general overview.
Often I find that exception handling is completely or at least partly omitted in custom coding. Many times developers spend all their time on functionality and have no time left over for handling errors. We know it is wrong, but nevertheless it happens time and again. Why? Some developers simply find the exception handling concept complicated and the numerous development options too overwhelming. Therefore, I decided to come up with a simple exception handling design for the Custom SAP ERP HCM Class Library. It consists of one exception class based on the IF_RECA_MESSAGE_LIST interface.
This is part 4 in a series of blogs on ABAP Objects in the context of a Custom SAP ERP HCM Class Library:
- ABAP Objects: Creating a Custom SAP ERP HCM Class Library
- ABAP Objects: Custom SAP ERP HCM Class Library – Example 1 – Class Instances
- ABAP Objects: Custom SAP ERP HCM Class Library – Example 2 – Utility Classes
- ABAP Objects: Custom SAP ERP HCM Class Library – Example 3 – Exceptions
While working with the design of the Custom SAP ERP HCM Class Library, I needed to find a common way of handling errors. I started searching SCN for ideas and found Uwe Schieferstein’s Wiki Message Handling – Finding the Needle in the Haystack. According to Uwe Schiefterstein, interface IF_RECA_MESSAGE_LIST is the ultimate message handler and I therefore decided to embed it into an exception class.
Exception Class ZCX_MESSAGES
In a previous blog ABAP Objects: Custom SAP ERP HCM Class Library – Example 2 – Utillity Classes, the CHECK_MANAGER method is called when instantiating a manager object of type ZCL_HR_MGR. In method CHECK_MANAGER exceptions of type ZCX_MESSAGES are raised. Lets have a closer look at the exception class ZCX_MESSAGES.
Exception class ZCX_MESSAGES is a subclass of CX_STATIC_CHECK and belongs to the utility package ZAPI_UTIL.
Besides the standard interfaces IF_MESSAGE and IF_SERIALIZABLE_OBJECT, I have added the custom interface ZIF_MESSAGE_HANDLING.
Attribute ZIF_MESSAGE_HANDLING~MO_MESSAGE_LIST comes from the ZIF_MESSAGE_HANDLING interface. It is not used in the ZCX_MESSAGES class. Instead attribute MESSAGES of type IF_RECA_MESSAGE_LIST is used in order to make the calling code easier to read. Finally, attribute ZCX_MESSAGES is added automatically when creating the text.
The following text is created for holding an IF_RECA_MESSAGE_LIST object. The attribute MESSAGES is used as a parameter in the text.
The CONSTRUCTOR is added automatically when creating the text. Method ZIF_MESSAGE_HANDLING~GET_MESSAGE_LIST simply returns the MESSAGES attribute of the ZCX_MESSAGES class.
Below you find an example program using the ZCX_MESSAGES class.
The example is simple and only one exception is caught and handled. The strength of the concept, however, is dealing with complex exception handling where numerous exceptions are raised and should be handled together. Whereas normal exception classes would terminate a program at the first occurrence of an exception, the concept above makes it possible to collect exceptions and handle these as a bundle. Using one general exception class enables passing on exceptions to the next calling level without having to think of catching and converting exceptions raised. Exceptions are only caught when adding exceptions to the collector, otherwise they are simply passed on.
All my custom programs use the same error handling. A benefit is that I can reuse code from other developments and that it is easier to maintain because it is always done the same way.