CATCH

CATCH Syntax diagram

Variants:
1. CATCH SYSTEM-EXCEPTIONS except1 = rc1 ... exceptn = rcn.

Effect

You can catch ABAP runtime errors in the processing block enclosed in the CATCH ... ENDCATCH statements.

Note the following:

Specifying a SYSTEM-EXCEPTION

You can specify the following as except1 ... exceptn:

Catchable runtime errors are assigned to ERROR classes. If you specify an ERROR class, all of its associated runtime errors will be caught. If you specify OTHERS, the system traps all catchable runtime errors.

Keyword dependency

Within CATCH ... ENDCATCH, a particular runtime error will only be caught if it is caused by a particular keyword.

You can find out the keywords with which you can catch a particular runtime error:

If the system catches a runtime error,

the current processing block is interrupted, and the program processing jumps directly from the statement where the error occurred to the ENDCATCH statement. This occurs irrespective of the number of control structures (IF, DO, LOOP, SELECT, CATCH, etc, ...) bypassed in so doing.
No guarantees can be made about the contents of any fields involved in the runtime error following the ENDCATCH statement.

The

return code

is set as follows:

After the ENDCATCH statement, SY-SUBRC is set to the corresponding value "rcn". This is the value assigned to the runtime error or ERROR class in the CATCH statement. If there is more than one "exceptn = rcn" expression in the runtime error, the "rcn" from the first expression is used. This is particularly significant if there are two ERROR classes which contain the same runtime error.
If the system does not catch a runtime error, the value of SY-SUBRC after the ENDCATCH statement is 0.

Example

Nested Structures
The example below calculates the factorial of the program parameter "fact ". If "fact" is too large, the system catches the runtime error COMPUTE_BCD_OVERFLOW . In this case, the system leaves the current DO...ENDDO loop at the MULTIPLY... statement, and jumps to the ENDCATCH statement.

PARAMETERS fact TYPE i.
DATA: fact_save TYPE i,
res(16) TYPE p.
*** ARITHMETIC_ERRORS contains COMPUTE_BCD_OVERFLOW ***
CATCH SYSTEM-EXCEPTIONS ARITHMETIC_ERRORS = 5.
res = fact_save = fact.
SUBTRACT 1 FROM fact.
DO fact TIMES.
MULTIPLY res BY fact. "<- COMPUTE_BCD_OVERFLOW
SUBTRACT 1 FROM fact.
ENDDO.
ENDCATCH.
IF sy-subrc = 5.
WRITE: / 'Overflow! Factorial of', fact_save,
'can not be calculated.'.
ELSE.
WRITE: / 'Factorial of', fact_save, 'gives', res.
ENDIF.

Examples

Keyword Dependency

In the first example, the runtime error CONVT_NO_NUMBER is caught during conversion with MOVE.
In the second example, the runtime error cannot be caught, since the keyword SELECT has no primary conversion function.
In the third example, the conversion takes place in an auxiliary field (MOVE 'abc' TO int.) andn the SELECT statement uses only operands with the same type. This enables the system to catch the runtime error.

DATA I TYPE I.
*** CONVERSION_ERRORS contains CONVT_NO_NUMBER ***
CATCH SYSTEM-EXCEPTIONS CONVERSION_ERRORS = 1.
MOVE 'abc' TO I. " <- Error: CONVT_NO_NUMBER
ENDCATCH.
IF SY-SUBRC = 1.
...
ENDIF.
...

TABLES SFLIGHT.
CATCH SYSTEM-EXCEPTIONS CONVERSION_ERRORS = 1.
SELECT * FROM SFLIGHT
WHERE SEATSMAX = 'abc'. " <- Error: CONVT_NO_NUMBER
...
ENDSELECT.
ENDCATCH.
...

TABLES SFLIGHT.
DATA int LIKE SFLIGHT-SEATSMAX.
CATCH SYSTEM-EXCEPTIONS CONVERSION_ERRORS = 1.
MOVE 'abc' TO int. " <- Error: CONVT_NO_NUMBER
SELECT * FROM SFLIGHT WHERE SEATSMAX = int.

...
ENDSELECT.
ENDCATCH.
...

Note

Using Runtime Error ID and ERROR Class

You are recommended only to use ERROR classes in the CATCH statement wherever possible. Assignment of keywords to ERROR classes contains a simple description of which errors are covered by a particular ERROR class.
You should only use the runtime error ID if the error situation can be very precisely defined. For more information about when runtime errors occur, see alphabetical list of all catchable runtime errors.

Note

Performance:

The CATCH...ENDCATCH statements require about
3-4 msn (standardized microseconds) of runtime, if no runtime error occurs.
If the statements then have to catch a runtime error, they may require more runtime, depending on the type of the runtime error. In the least serious case, this will be another 3-4 msn. In more serious cases, it can be up to 20 msn. For most cases, the increased runtime will be around 5-10 msn.