Class-Based Exceptions
An exception is a situation that arises while a program is being executed during which there is no point to continue the normal program flow. SAP NetWeaver Application Server (SAP NetWeaver AS) S 6.10 introduced a new ABAP Objects exception concept that exists parallel to the existing concept based on sy-subrc. Exceptions and exception handling are now based on classes. This new ABAP Objects exception concept enhanced the classic way of handling exception using sy-subrc.
Note that the use of class-based exceptions is not limited to object-oriented contexts. Class-based exceptions can be raised and handled in all processing blocks. In particular, all previously catchable runtime errors can be handled as class-based exceptions.
In the new exception concept, an exception is represented by an exception object. An exception object is an instance of an exception class. The attribute values of the exception object contain information about the respective error situation. Raising a class-based exception means instantiating an exception class and setting the attributes. Handling a class-based exception involves evaluating the exception object and its attribute values.
Class-based exceptions are raised either by the RAISE EXCEPTION statement or by the runtime environment. You catch and handle class-based exceptions with the TRY...CATCH...ENDTRY structure.
Hierarchy of Exception Classes
You can define your own exception classes, but the system already includes a range of predefined exception classes – particularly for exceptions in the runtime environment. You can create exception classes globally in the Class Builder, but you can also define local exception classes within a program or global class.
The names of global exception classes always start with <namespace>CX_. Those global exception classes that the runtime environment uses start with CX_SY_. We recommend that you start the names of local exception classes with LCX_.
All exception classes are derived from one exception class, CX_ROOT. Therefore, you can generically access any exception object through a reference variable, REF TO CX_ROOT.
However, a new exception class is not allowed to inherit directly from CX_ROOT, derive any new exception class directly or indirectly from one of the subclasses CX_ROOT - CX_NO_CHECK, CX_DYNAMIC_CHECK, or CX_STATIC_CHECK.
Through this, all exception classes are subdivided into three groups. Depending on the group to which a given exception belongs, the exception is treated differently by syntax check and runtime environment. The default group is CX_STATIC_CHECK, which ensures maximum syntax check and program stability. Use the other groups only in special cases.
The GET_SOURCE_POSITION method returns the name of the main program or include program and also the line number in the source code where the exception occurs. The GET_TEXT method returns an exception text in the form of a string. This method is not defined in CX_ROOT directly but in interface IF_MESSAGE, which is implemented by CX_ROOT.
Class-Based Exception Handling
If an exception occurs, the system searches for a matching CATCH statement in the TRY-ENDTRY structure that immediately surrounds the statement. It searches through the CATCH blocks for the relevant exception class or the superclasses from the inheritance hierarchy. If any of the relevant exception classes is found, the program navigates directly to the handler.
If the system does not find a matching CATCH statement, it gradually searches outwards in the surrounding TRY-ENDTRY structures. If no handler can be found within the same procedure, the system tries to propagate the exception to the calling program. This process will be discussed in more detail later.
If a TRY-ENDTRY structure contains a CLEANUP block, this block is executed when the TRY-ENDTRY structure is exited because the system cannot find a handler within the TRY-ENDTRY structure itself but instead in a surrounding TRY-ENDTRY structure or in a calling program.
You can handle an exception if the statement that raised it is enclosed inside a TRY-ENDTRY control structure. You handle the exception using the CATCH statement in the TRY-ENDTRY structure.