SAP is in an interesting spot with ABAP. There are a lot of newer and better ways to things, but a lot of old things also work. Trying to get the benefits of the new ways while using existing business logic which was written the old way always throws up some fun.
In this particular case, I will show you the solution that I have come up with to handle the old style exceptions thrown by a function module call. This method is completely generic and should work with any function module call that throws classic exceptions.
The new exception class
Create a new exception class that you will use to "translate" these messages from one format to another. In this class, you have to add 5 new attributes to carry your Message Type and Message Variables.
If you are doing this with the Eclipse environment, this is what you would declare.
CLASS zcx_my_exception DEFINITION
INHERITING FROM cx_static_check
CREATE PUBLIC .
INTERFACES if_t100_message .
"Start of addition
DATA msgty TYPE symsgty .
DATA text1 TYPE sylisel .
DATA text2 TYPE sylisel .
DATA text3 TYPE sylisel .
DATA text4 TYPE sylisel .
"End of addition
!textid LIKE if_t100_message=>t100key OPTIONAL
!previous LIKE previous OPTIONAL
!msgty TYPE symsgty OPTIONAL
!text1 TYPE sylisel OPTIONAL
!text2 TYPE sylisel OPTIONAL
!text3 TYPE sylisel OPTIONAL
!text4 TYPE sylisel OPTIONAL .
The calling Methods
In this code sample, I have the key pieces of code that you will need. Please don't pay attention to the fact that the variables and import - export parameters are not declared.
When declaring the class, all the methods have to be declared with the RAISING zcx_my_exception. This ensures that any exception that is raised is passed to the next level.
CLASS lcl DEFINITION.
Standard Function Modules
First the standard function call. I have an additional method GET_MASTER_DATA( ) which is just an additional layer to show how exceptions can be handled at the highest level. For those of you who haven't used class based exceptions in ABAP before, this is to give you a glimpse of the power and simplicity of using this over the traditional method of passing and handling the exception at every level.
CLASS lcl IMPLEMENTATION.
"Prep the material number, etc
"Post call routine - Do something with data
A couple of points of note here. Always declare the extra catch all exception ERROR_MESSAGE (in this case = 25). This is a safety in case the person who programmed the function module raised an exception that was not declared. I have had crashes when I forgot to do this.
The other is the fact that I am also passing the MSGTY (Message Type). Even though the new class based exceptions are "Typeless", because you raise different ones in different circumstances, I sometimes find it useful to have this field around. This is a personal preference, because you can certainly make the argument that all exceptions out of a function module are errors.
Using a BAPI
is similar, but instead of the SYST fields, you will be using the data from the Bapi return table.
bapiret_tab TYPE STANDARD TABLE OF bapiret2.
CALL FUNCTION 'BAPI_PRODORD_CREATE'
orderdata = orderdata " Transfer Structure for Creating Production Orders
return = bapiret_tab " Return Parameters
order_number = e_order_number. " Production order number
The LOOP on the bapiret_tab is just one way of filtering the table. You can of course use the READ TABLE or table expressions (bapiret = bapiret_tab[ type = 'E' ] ) to achieve the same. This is where the use of the MSGTY gets interesting because a lot of BAPI's do send back the full gamut of Warnings and Success messages too. Some time in the future, I would like to experiment with making this RAISE a RESUMABLE one, and using that to log the Success and Warning messages.
Using the exceptions
This is the easy part! Just like any other TRY... CATCH.
DATA(obj) = NEW lcl( ).
CATCH zcx_my_exception INTO DATA(ocx_my).
" Do some clean up work
" Show the user the error message
MESSAGE ID ocx_my->if_t100_message~t100key-msgid
The only tricky part is how you get the MSGID and MSGNO from the exception object that has been caught - you have to go through the interface for accessing the T100 texts. So, that is a couple of layers deep.