SAP ABAP - Exception Handling
SAP ABAP - Exception Handling
SAP ABAP - Exception Handling
An exception is a problem that arises during the execution of a program. When an exception
occurs the normal flow of the program is disrupted and the program application terminates
abnormally, which is not recommended, therefore these exceptions are to be handled.
Exceptions provide a way to transfer control from one part of a program to another. ABAP
exception handling is built upon three keywords − RAISE, TRY, CATCH and CLEANUP. Assuming
a block will raise an exception, a method catches an exception using a combination of the TRY and
CATCH keywords. A TRY - CATCH block is placed around the code that might generate an
exception. Following is the syntax for using TRY – CATCH −
TRY.
CATCH
. . .
. . .
. . .
CATCH
CLEANUP.
ENDTRY.
RAISE − Exceptions are raised to indicate that some exceptional situation has occurred. Usually,
an exception handler tries to repair the error or find an alternative solution.
TRY − The TRY block contains the application coding whose exceptions are to be handled. This
statement block is processed sequentially. It can contain further control structures and calls of
procedures or other ABAP programs. It is followed by one or more catch blocks.
CATCH − A program catches an exception with an exception handler at the place in a program
where you want to handle the problem. The CATCH keyword indicates the catching of an
exception.
CLEANUP − The statements of the CLEANUP block are executed whenever an exception occurs
in a TRY block that is not caught by the handler of the same TRY - ENDTRY construct. Within the
CLEANUP clause, the system can restore an object to a consistent state or release external
resources. That is, cleanup work can be executed for the context of the TRY block.
Raising Exceptions
https://www.tutorialspoint.com/sap_abap/sap_abap_exception_handling.htm 1/4
7/31/22, 7:58 PM SAP ABAP - Exception Handling
Exceptions can be raised at any point in a method, a function module, a subroutine, and so on.
There are two ways an exception can be raised −
For instance Y = 1 / 0. This will result in a run time error of type CX_SY_ZERODIVIDE.
Raise and create an exception object simultaneously. Raise an exception with an exception
object that already exists in the first scenario. The syntax is: RAISE EXCEPTION exep.
Catching Exceptions
Handlers are used to catch exceptions.
TRY.
msgtxt = exref→GET_TEXT( ).
msgtxt = exref→GET_TEXT( ).
In the above code snippet, we are trying to divide Num1 by Num2 to get the result in a float type
variable.
Attributes of Exceptions
Here are the five attributes and methods of exceptions −
https://www.tutorialspoint.com/sap_abap/sap_abap_exception_handling.htm 2/4
7/31/22, 7:58 PM SAP ABAP - Exception Handling
1
Textid
Used to define different texts for exceptions and also affects the result of the method
get_text.
2
Previous
This attribute can store the original exception that allows you to build a chain of
exceptions.
3
get_text
This returns the textual representation as a string as per the system language of the
exception.
4
get_longtext
This returns the long variant of the textual representation of the exception as a string.
5
get_source_position
Gives the program name and line number reached where the exception was raised.
Example
REPORT ZExceptionsDemo.
start-of-selection.
write: /.
TRY.
ENDIF.
TRY.
res_1 = 1 / Num_1.
https://www.tutorialspoint.com/sap_abap/sap_abap_exception_handling.htm 3/4
7/31/22, 7:58 PM SAP ABAP - Exception Handling
txt_1 = orf_1→GET_TEXT( ).
CLEANUP.
CLEAR res_1.
ENDTRY.
txt_1 = orf_1→GET_TEXT( ).
txt_1 = orf_1→GET_TEXT( ).
ENDTRY.
Write / txt_1.
ENDIF.
In this example, if the number is greater than 150, the exception CX_DEMO_ABS_TOO_LARGE is
raised. The above code produces the following output for the number 160.
https://www.tutorialspoint.com/sap_abap/sap_abap_exception_handling.htm 4/4