To understand an exception class, we need to understand what is an exception and when it is raised.
When we execute any program, some discrepancies can occur due to which a normal flow of execution gets interrupted and the behavior will not be the same as it was expected. That discrepancy is like an event which is occurring due to some reason, which is known as an ‘Exception’.
It is required to handle such discrepancies in order to prevent the execution flow of any program. In order to achieve that, SAP ABAP has a special class known as an ‘Exception Class’.
Exception Classes can be defined in a global class as well as in a local class. There are three exception classes exists, which are the subclasses of one root class ‘CX_ROOT’. Those three exception classes are:
- CX_STATIC_CHECK – which is being checked by both compiler and runtime system.
- CX_DYNAMIC_CHECK – Checked only at runtime system. All System exceptions will come under this exception.
- CX_NO_CHECK – This type will be chosen if exceptions are frequent.
An Exception class is used to handle the discrepancies(exception) occurs during the execution a of program.
Whenever an exception occurs, an object is created (known as an exception object). The attributes of this object would contain information about the error. This exception can be caught by using TRY….CATCH…..ENDTRY block.
TRY….ENDTRY block contains a protected area, where an exception can be caught using the CATCH statement. One TRY….ENDTRY block can catch more than one exception, but it should get caught in a sequence in which it occurs.
There are two types of exception handling cases:
- One where the exception was raised, deleted before or after handling it.
- The other, where an exception which was raised is retained and the program is resumed after the statement that raised an exception.
Let’s take an example of a simple arithmetic operation of two numbers. An exception should be raised if one of the two operands is missing.
NOTE: A local class is used in below sample program.
CLASS lcx_missing_operand DEFINITION
INHERITING FROM CX_STATIC_CHECK.
ENDCLASS. “lcx_missing_operand DEFINITION
CLASS lcl_test_exception DEFINITION.
METHODS: add_numbers IMPORTING iv_num1 TYPE i OPTIONAL
iv_num2 TYPE i OPTIONAL
RETURNING VALUE(rv_calc) TYPE i
METHODS: do_check RAISING lcx_missing_operand.
DATA: lv_num1 TYPE i,
lv_num2 TYPE i,
lv_result TYPE i.
ENDCLASS. “lcl_test_exception DEFINITION
CLASS lcl_test_exception IMPLEMENTATION.
v_num1 = iv_num1.
v_num2 = iv_num2.
rv_calc = v_result.
IF v_num1 IS INITIAL.
RAISE EXCEPTION TYPE lcx_missing_operand.
ENDCLASS. “lcl_test_exception IMPLEMENTATION
DATA: lo_obj TYPE REF TO lcl_test_exception.
DATA: lo_exc_root TYPE REF TO CX_ROOT.
DATA: lv_msg TYPE STRING.
CREATE OBJECT lo_obj.
lo_obj->add_numbers( iv_num1 = 10 ).
WRITE: / ‘Operand is missing’.
CATCH CX_ROOT INTO lo_exc_root.
lv_msg = lo_exc_root->get_text( ).
WRITE: / lv_msg.
- With exception classes, you can track the sequence of called methods. Since in exception class, we have try-end-try block, so we can put the called methods in it, to know if one of them get failed.
- An exception class has an advantage that non-OO programmers won’t understand your code, increasing your job security a little.