An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. We can handle the exception with the help of Exception Classes.
Concept of exception classes in ABAP
In ABAP class based exceptions are raised either by RAISE EXCEPTION statement or by the runtime environment.
All the system-defined exception classes are derived from one of the classes CX_NO_CHECK,CX_DYNAMIC_CHECK or CX_STATIC_CHECK. These classes are , in turn, derived from common superclass CX_ROOT.
The CX_ROOT class provides some predefined methods that are inherited by all the exception classes.
All Exception classes are inherited from the global exception class’s CX_ROOT these subclasses:
- CX_STATIC_CHECK – If the exception is defined as a subclass of CX_STATIC_CHECK, it has to be declared in the method or FM signature. If the exception is propogated from the method or FM, it has to be explicitly declared in all the FM or method signature. If the exception is not declared, it has to be caught. If not caught neither declared, it would result into run-time error.
- CX_DYNAMIC_CHECK – If the exceptions are inherited from CX_DYNAMIC_CHECK, they also needs to be declared in the FM signature. If not, they wont be checked at design time. Generally, all system exceptions like CX_SY_ZERO_DIVIDE begins with CX_SY. All of them are inherited from CX_DYNAMIC_CHECK.
- CX_NO_CHECK – Exceptions that are defined using subclasses of CX_NO_CHECK must not be declared explicitly in the interface of the procedure. The class CX_NO_CHECK and its subclasses are implicitly always declared and are always propagated.
Visit this Class-based Exceptions II – Design Time consideration for more information on what we need to keep in mind while designing the exceptions using flow diagram.
Handling an Exception
When an exception situation is raised, the exception is represented by an Exception Object. It contains all the information about the exception.
The exception is handled inside TRY .....ENDTRY block. This block contains CATCH statement. The CATCH statement contains the handling statements for an exception. If an exception occurs within any TRY .....ENDTRY block, system searches for a CATCH statement containing the handler statements of the exception in the same TRY .....ENDTRY block. If it finds any, the exception is handled accordingly.
If it doesn't find any of the TRY.. ENDTRY with CATCH statement for an exception or any higher of its super class, it would result into short-dump. Check out Class based Exceptions III – Runtime flow for more information on exception handling with flow diagram.