Introduction

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:

  1. CX_STATIC_CHECK – which is being checked by both compiler and runtime system.
  2. CX_DYNAMIC_CHECK – Checked only at runtime system. All System exceptions will come under this exception.
  3. CX_NO_CHECK – This type will be chosen if exceptions are frequent.

Definition

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:

  1. One where the exception was raised, deleted before or after handling it.
  2. The other, where an exception which was raised is retained and the program is resumed after the statement that raised an exception.

Program

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.

REPORT ztest_exception.

CLASS lcx_missing_operand DEFINITION

INHERITING FROM CX_STATIC_CHECK.

ENDCLASS.                                                 “lcx_missing_operand  DEFINITION

 

CLASS lcl_test_exception DEFINITION.

PUBLIC SECTION.

METHODS: add_numbers IMPORTING iv_num1 TYPE i OPTIONAL

iv_num2 TYPE i OPTIONAL

RETURNING VALUE(rv_calc) TYPE i

RAISING   lcx_missing_operand

PRIVATE SECTION.

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.

METHOD add_numbers.

v_num1 = iv_num1.

v_num2 = iv_num2.

me->do_check( ).

rv_calc = v_result.

ENDMETHOD.

 

METHOD do_check.

IF v_num1 IS INITIAL.

RAISE EXCEPTION TYPE lcx_missing_operand.

ENDIF.

ENDMETHOD.

 

ENDCLASS.                                                      “lcl_test_exception IMPLEMENTATION

 

START-OF-SELECTION.

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.

TRY .

lo_obj->add_numbers( iv_num1 = 10 ).

CATCH lcx_missing_operand.

WRITE: /  ‘Operand is missing’.

CATCH CX_ROOT INTO lo_exc_root.

lv_msg = lo_exc_root->get_text( ).

WRITE: / lv_msg.

ENDTRY.

Advantages

  1. 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.
  2. An exception class has an advantage that non-OO programmers won’t understand your code, increasing your job security a little.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Translate »