SAP USE EXCEPTION CLASS - Guide
Get Example source ABAP code based on a different SAP table
Using Exception Classes
ABAP_BACKGROUND
The concept of freely definable exception classes involves being able to create an exception class that adequately describes the exception situation in question. The description consists both of the name of the exception class, the associated exception texts, and their documentation. You can divide an exception class into multiple subexceptions by creating multiple exception texts. Subclasses of exception classes can be used to make the information even more specific.
ABAP_RULE
When describing an error situation, only use exception classes with the correct category and name, the appropriate attributes and texts, and which contain the correct documentation. Do not reuse inappropriate exception classes.
ABAP_DETAILS
Reusing existing exception classes with the wrong content removes all benefits of freely definable exception classes. The new exception object cannot describe the exception situation adequately. It also makes it very difficult to maintain and analyze the code. In particular, you run a great risk of handling the exception incorrectly. This is because a caller layer higher up in the hierarchy never expects the exceptions it handles to be raised by a situation with the wrong semantics.
The following procedure is recommended for raising correct exceptions: Search for an existing exception class that is released for use in the current concept (and as part of the package concept) and which matches that error situation exactly. Make an existing, almost ideal exception class more specific by passing on and/or adding new exception texts. Create an new ideal exception class, possibly as a part of a predefined inheritance hierarchy.
In doing so, it must be ensured that an appropriate
ABAP_EXAMPLE_BAD
The following source code shows the incorrect use of the system class
PUBLIC SECTION.
METHODS calculate_storage_capacity
RAISING cx_sy_arithmetic_error.
ENDCLASS.>
METHOD calculate_storage_capacity.
...
RAISE EXCEPTION TYPE cx_sy_arithmetic_overflow.
...
ENDMETHOD.
ENDCLASS.>
ABAP_EXAMPLE_END
ABAP_EXAMPLE_GOOD
The following source code shows how an application-specific exception class is used that has been created especially for the situation and whose name reflects the topic.
INHERITING FROM cx_static_check.
ENDCLASS.>
PUBLIC SECTION.
METHODS calculate_storage_capacity
RAISING cx_warehouse_out_of_capacity.
ENDCLASS.>
METHOD calculate_storage_capacity.
...
RAISE EXCEPTION TYPE cx_warehouse_out_of_capacity.
...
ENDMETHOD.
ENDCLASS.>
ABAP_EXAMPLE_END