Example: Class based exceptions with exception chaining
Introduction
In order to "chain" Exceptions in the ABAP world each exception class has an optional Parameter called PREVIOUS in their constructor which you can provide when raising your exception. By doing this you will build a so called "Exception Chain".
You can think of it like the forensics of a catastrophe. Usually catastrophes happen due to an Unfortunate series of events.
In the context of a software developer this is exactly the use case for those"Exception Chains".
Example
Let’s think about following use case:
You have a API which is able to change a specific head_text of a sales order.
There are many things that could go possibly wrong.
Of course the abstract problem is that the Sales order could not be changed. But why?
The reason for this could be that the sales order has been locked by another user.
Or the given sales order number does not exist.
This is a simple but easy-to-understand example for Exception Chaining.
How would you build this in ABAP? For this example we would have two exception classes:
ZCX_SALESORDER_TRANSACTION and ZCX_SALESORDER
The coding could look like this:
Let’s now take a look at Swagger UI (Open API Version 3.0):
When we execute it and the actual error happens we will get the following HTTP 500 Response:
Here the full JSON Response:
{ "DESCRIPT": "Salesorder Transaction", "EXCEPTION_ATTRIBUTES": { "PREVIOUS": { "TEXTID": "B7F5F45E4DEA9A51E10000000ADC9967", "UNAME": "WOLF", "VBELN": "0000005021" }, "TEXTID": "9EF5F45E4DEA9A51E10000000ADC9967", "VBELN": "0000005021" }, "EXCEPTION_CLASS": true, "EXCEPTION_ID": "ZCX_SALESORDER_TRANSACTION", "EXCEPTION_TEXT_ID": "SALESORD_COULD_NOT_BE_CHANGED", "MSG": { "MSGV1": "0000005021" }, "PREVIOUS": { "DESCRIPT": "Salesorder Errors", "EXCEPTION_ATTRIBUTES": { "TEXTID": "B7F5F45E4DEA9A51E10000000ADC9967", "UNAME": "WOLF", "VBELN": "0000005021" }, "EXCEPTION_CLASS": true, "EXCEPTION_ID": "ZCX_SALESORDER", "EXCEPTION_TEXT_ID": "SALESORD_LOCKED_BY_ANOTHER_USR", "MSG": { "MSGV1": "WOLF", "MSGV2": "0000005021" }, "SOTR_PARAMS": [ { "PARAM": "UNAME", "VALUE": "WOLF" }, { "PARAM": "VBELN", "VALUE": "0000005021" } ], "SYSTEM": "NAD", "TEXT": "Salesorder 5021 is currently locked by WOLF" }, "SOTR_PARAMS": [ { "PARAM": "VBELN", "VALUE": "0000005021" } ], "SYSTEM": "NAD", "TEXT": "Salesorder 5021 could not be changed" }
How could this look like in ABAP?
When you don’t want to catch it you get of course a DUMP which can be reviewed with ST22.
If you had to catch it and want to handle it this could look something like this (below example also highlights the many ways to handle class based exceptions in ABAP):