Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
AlwinPut
Active Participant
5,293

Don't use this class anymore. Use ZCX_RETURN3.


See blog: ABAP Exception Class ZCX_RETURN3

Introduction


As an ABAP programmer we must program Object Oriented code (aka OO code). So for exception / error handling we need to use Exception classes instead of using the old procedural ways of error handling like SY-SUBRC and BAPIRETURN data.

However often we have to call BAPIs, or less favorite other function modules or FORM routines. In the new OO code we create we have to handle the procedural errors and convert them to OO Exceptions.

Some years ago I started to create a generic exception class for handling all kind of procedural error messages (old procedural and new OO messages) and combining it with the good old SAP messages (T: SE91).

This class is called ZCX_RETURN2. It has many ways for handling all kind of procedural errors like SY-SUBRC, SY-MSGID / MSGNO and all kind op BAPIRETURN types.

In this blog post is explained how it works. It also contains references to related blogs for code templates for quickly adding error handling code.

Examples


Probably most used exception class instantiations are for:

  • BAPI errors

  • SE91 message errors


Therefor the two examples below will show how this works.

Example 1: Raising BAPI errors


For example you call a BAPI function module routine which returns a BAPI return table of type BAPIRET2.
        DATA:
lt_return TYPE bapiret2_t.

CALL FUNCTION 'BAPI_USER_CREATE1'
EXPORTING
username = 'DUMMY'
logondata = ls_logondata
password = ls_password
address = ls_address
TABLES
return = lt_return.

DATA(lx_return) =
zcx_return2=>create_by_bapiret2_table( lt_return ).

IF lx_return IS NOT INITIAL.
RAISE EXCEPTION lx_return.
ENDIF.

The return table variable LT_RETURN will receive the error messages from the BAPI.

Method CREATE_BY_BAPIRET2_TABLE will instantiate ZCX_RETURN2.

If LT_RETURN contains one or more messages with type E, A or X, then method CREATE_BY_BAPIRET2_TABLE will create an instance.

After the instantiation call, LX_RETURN will be checked if it has an instance. If so, than the Exception object will be raised.

Example 2: Raising SAP and custom message


        SELECT SINGLE
kunnr
FROM kna1
WHERE kunnr = @gv_kunnr
INTO @DATA(ls_kna1).

IF sy-subrc <> 0.

"Customer &1 not found.
MESSAGE e001
WITH gv_kunnr
INTO DATA(lv_dummy) ##NEEDED.

DATA(lx_return) = zcx_return2=>create_by_system_message( ).
RAISE EXCEPTION lx_return.

ENDIF.

Function modules, FORM routines and ABAP statements can fill SY-SUBRC. If the value is <> 0, then an error occurred.

If the system variables (SY) MSGID, MSGNO and MSGVn are not yet filled, then the programmer has to fill it by using the MESSAGE statement like in this example.

Method CREATE_BY_SYSTEM_MESSAGE will now instantiate the ZCX_RETURN2 class. And after that it will be raised as exception.

The essence of the ZCX_RETURN2


The class ZCX_RETURN2 converts all kinds of messages to its own attribute internal table GT_RETURN of type BAPIRET2_T.



It has many ways of instantiating the class.

For retrieving the data use the GET-methods:

  • IF_MESSAGE~GET_TEXT
    Get the message text of the first error.

  • IF_MESSAGE~GET_LONGTEXT
    Get the long text of T:SE91 of the first error.

  • GET_BAPIRET2_STRUC
    Get the first error of GT_RETURN.

  • GET_BAPIRET2_TABLE
    Get all errors from table GT_RETURN.


Raising the ZCX_RETURN2 exception


Instantiating the exception class is key for using the class. Instantiating this class in different ways is done by public static methods. So it has many so called factory methods.

For every instantiation method a code templates is created, so you can quickly copy and paste it to implement the code.

The factory methods are:


Blog post: ABAP Exception Class ZCX_RETURN2 – Raise code templates

Catching the ZCX_RETURN2 exception


This can be done for different purposes like ABAP programs / reports, ABAP Gateway, ABAP Proxy, IDocs and RFC function modules.

Blog post: ABAP Exception Class ZCX_RETURN2 – Catch code templates

Download and install ZCX_RETURN2


Download



Install ZCX_RETURN2 class


Option 1: with Eclipse (this is the preferred way)


This is the preferred way, because in Eclipse all code can be copied and pasted at once.

  • Determine or create an ABAP package for the class ZCX_RETURN2.

  • Create a new class: ZCX_RETURN2.

  • Copy and paste the code of the ZCX_RETURN2.abap file.


Option 2: with ABAP workbench (not the preferred way)



  • Determine or create an ABAP package for the class ZCX_RETURN2.

  • Create in SE80 class: ZCX_RETURN2.
    Super class: CX_STATIC_CHECK
    Description: RETURN2 exception class
    Inst. Generation: PublicClass type: Exception class
    With messages of message classes as exception texts: OFF
    Final: OFFPackage: ZCA_DOMAIN

  • Activate the class.

  • Menu: Goto -> Sections -> Public section.

  • Definition part

    • Copy and paste the Definition part of the file ZCX_RETURN2 into SE80 editor



  • Implementation part.

    • Double click on every method and copy and paste the ABAP code.
      Use the Panel on the left (Explorer view) to navigate to the next method.




Appendix: Raising exceptions from procedural routines


Creating new procedural routines is not allowed anymore unless you need it for technical reasons. For example.

  • Function module might be needed for tRFC, qRFC, remote enabled or update task.

  • New FORM routines are not allowed.

  • New Macros are not allowed.

  • ABAP program events are allowed, because they are needed for technical reasons.


In the procedural routine only calling methods is allowed and catching OO exceptions for these methods. It is not allowed to raise OO exceptions.

Appendix: ZCX_RETURN2 versioning


It is already version 2, therefor the 2 in the name. In comparison the version 1, the naming of the methods have been improved and the coding is refactored by consistency of doing creating the instance and as a result it is simplified.

ABAP BAPI BO Class Generator still uses an older version named ZCX_RETURN.

Appendix: SAP messages (T:SE91) advantages and tips


Advantages



  1. By using the Where-used list in T: SE91, you can find the ABAP code location.

  2. They have a name / number combination which identify each message uniquely.

  3. They can be translated.

  4. They can handle 4 variables.

  5. They can be added to the Application log (T: SLG1 / SLG0).

  6. They are used by all BAPI RETURN structure types and table types.


Tips



  1. Make a Message class per ABAP class.

  2. Do not reuse messages for same message on multiple locations.
    It might be a sign that you do have the same code on multiple places and that it should be refactored.

  3. Do not use messages like &1 &2 &3 &4. Make every message specific.

3 Comments
Labels in this area