Show TOC

Declaring Exceptions from a Raise StatementLocate this document in the navigation structure

Context

You can add an exception to the method signature based on an existing RAISE EXCEPTION statement of the method implementation.

Example

In the implementation of the method drive, the exception speed_too_high is raised. This is to be reflected in the method signature by adding the exception to the RAISING clause. The exception class cx_ris_exception that relates to the exception raise_statement is to be defined in the public section:

CLASS cl_car_with_exception DEFINITION PUBLIC.
 PUBLIC SECTION.
  METHODS:
   drive
    IMPORTING
     i_speed TYPE i.
ENDCLASS.

CLASS cl_car_with_exception IMPLEMENTATION.
 METHODS drive.
  IF i_speed > 240.
   RAISE EXCEPTION TYPE cx_speed_too_high.
  ENDIF.
 ENDMETHOD.
ENDCLASS.

Procedure

  1. In the implementation part, select the exception class that you want to define in the method signature.
  2. In the context menu, choose Quick Fix or use the shortcut (Ctrl 1).
  3. In the Quick Fix dialog box, double-click Add raising declaration.

Results

In the signature of the drive method, the exception class cx_speed_too_high is added to the RAISING clause.

CLASS cl_car_with_exception DEFINITION PUBLIC.
 PUBLIC SECTION.
  METHODS:
   drive
    IMPORTING
     i_speed TYPE i.
    RAISING
     cx_speed_too_high.
ENDCLASS.

CLASS cl_car_with_exception IMPLEMENTATION.
 METHODS drive.
  IF i_speed > 240.
   RAISE EXCEPTION TYPE cx_speed_too_high.
  ENDIF.
 ENDMETHOD.
ENDCLASS.