Show TOC

Surrounding with TRY CATCHLocate this document in the navigation structure

You can surround the selected block of statements with a TRY CATCH statement in order to handle raised exception(s) as a single or multiple TRY CATCH block.

Prerequisites

This functionality is provided since SAP NetWeaver 7.5 SP00.

Context

Example with Single CATCH

Before Execution After Execution

To copy the source code example, click here Code Example Before Execution

In the implementation of the drive method , the check_fuel and check_engine methods are called. The check_fuel and check_engine methods raise an exception of the cx_failure and cx_no_fuel types. The signature of the drive method does not contain any exception.

In the drive method, the selection contains the call of the check_fuel and check_engine methods that can raise exceptions. You want to handle the cx_no_fuel and cx_failure exceptions that might occur in the drive method.

In the drive method, the selection contains the call of the check_fuel and check_engine methods that can raise exceptions. You want to handle the cx_no_fuel and cx_failure exceptions that might occur in the drive method.

Example with Multi CATCH

Before Execution After Execution

To copy the source code example, click here Code Example Before Execution

In the implementation of the drive method , the check_fuel and check_engine methods are called. The check_fuel and check_engine methods raise an exception of the cx_failure and cx_no_fuel types. The signature of the drive method does not contain any exception.

A TRY CATCH block is added to the drive method. If you have selected the call, a CATCH block for each exception (cx_no_fuel and cx_failure) is added. Inside each CATCH block, you can add code to handle the exceptions.

To handle this, ABAP Development Tools (ADT) provides two options:

Procedure

  1. In the implementation part, select the souce code that can raise one or more exceptions.
  2. Get the quick assist proposals by choosing Quick Fix from the context menu (shortcut Ctrl 1) or by using the Quick Assist view.
  3. Apply Surround all with TRY CATCH. If you have selected Surround all with TRY MULTI CATCH, all raised exceptions are added to one CATCH block.
    Note In SAP NetWeaver 7.4 SP08 and higher, you can also select several methods that are called. You then need to choose Surround all with TRY MULTI CATCH in order to handle all raised exceptions that are defined for the selected method(s) in the public section. Thus, all of the raised exceptions are added and handled in one CATCH block.