ABAP Keyword Documentation → ABAP Programming Guidelines → Architecture → Error Handling
Using Exception Classes
Other versions: 7.31 | 7.40 | 7.54
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.
Rule
Only use suitable exception classes
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.
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, where you must take care that the right exception category is used:
Bad Example
The following source code shows the incorrect use of the system class cx_sy_arithmetic_overflow
(which exists in every system) for an application-specific exception situation. This system exception should usually only be raised by the ABAP runtime environment when an arithmetic calculation takes place.
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.
Good Example
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.
CLASS cx_warehouse_out_of_capacity DEFINITION
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.