METHODS - IMPORTING, EXPORTING, CHANGING, RAISING
Short Reference
ABAP Syntax_1 METHODS meth [ABSTRACT|FINAL]
[IMPORTING parameters [PREFERRED
PARAMETER p]]
[EXPORTING parameters ]
[CHANGING parameters ]
[{RAISING exc1|RESUMABLE(exc1) exc2|RESUMABLE(exc2) ...}
|{EXCEPTIONS exc1 exc2 ...}].
ABAP_ADDITIONS:
1 ... IMPORTING parameters [PREFERRED PARAMETER p]
2 ... EXPORTING parameters
3 ... CHANGING parameters
4 ... RAISING exc1|RESUMABLE(exc1) exc2|RESUMABLE(exc2) ...
5 ... EXCEPTIONS exc1 exc2 ...
6 ... ABSTRACT ...
7 ... FINAL ...
What does it do? This statement declares a general instance method
meth . Use the additions ABSTRACT and FINAL to make the
method abstract or final.
The additions IMPORTING , EXPORTING and CHANGING
define the parameter interface of
the method. After every addition, the corresponding formal parameters
are defined by a specification of the list
parameters . The order of the additions is fixed.
The remaining additions define which exceptions are propagated or
raised by the method.
Latest notes: Within a method, the
predicate expression IS SUPPLIED
can be used to check whether an optional formal parameter was assigned
an actual parameter when it was called.
ABAP_ADDITION_1 ... IMPORTING parameters [PREFERRED PARAMETER p]
What does it do? IMPORTING defines input parameters. When the
method is called, an appropriate actual parameter must be specified for
every non-optional input parameter. The content of the actual parameter
is passed to the input parameter when the call is made. The content of
an input parameter for which pass-by-reference is defined cannot be
changed in the method.
Use PREFERRED PARAMETER to identify an input parameter p1
p2 ... from the list parameters
after IMPORTING as a preferred parameter. This can only be
specified if all input parameters and input/output parameters are
optional. The parameter specified after PREFERRED PARAMETER is
set to optional implicitly. If the method is called using the syntax
meth( a ) ( standalone or
functional ), the actual parameter
a is assigned to the preferred input parameter p .
Latest notes: Although PREFERRED PARAMETER makes the parameter
p implicitly optional, this parameter should be made explicitly
optional using OPTIONAL or DEFAULT . Otherwise a syntax
check warning is displayed.
ABAP_ADDITION_2 ... EXPORTING parameters
What does it do? EXPORTING defines output parameters. When the
method is called, an appropriate actual parameter can be specified for
every output parameter. The content of an output parameter defined for
pass-by-value is passed to the actual parameter after the method has
been completed successfully.
Latest notes: An output parameter defined for pass-by-reference behaves
like an input/output parameter, which means that it is not initialized
when the method is called. For this reason, it should not be read before
the first write access. In addition, be careful when adding content to
such parameters as, for example, when inserting rows into internal
tables.
ABAP_ADDITION_3 ... CHANGING parameters
What does it do? CHANGING defines input/output parameters. When the
method is called, an appropriate actual parameter must be specified for
every non-optional input/output parameter. The content of the actual
parameter is passed to the input/output parameter at the call, and after
the method has been completed, the content of the input/output parameter
is passed to the actual parameter.
Example ABAP Coding The method read_spfli_into_table of this example
has an input and an output parameter, which are typed fully by reference
to ABAP Dictionary.
CLASS flights DEFINITION.
PUBLIC SECTION.
METHODS read_spfli_into_table
IMPORTING VALUE(id) TYPE spfli-carrid
EXPORTING flight_tab TYPE spfli_tab.
...
ENDCLASS.
ABAP_ADDITION_4 ... RAISING exc1|RESUMABLE(exc1)
exc2|RESUMABLE(exc2) ...
What does it do? Use the addition RAISING to declare the
class-based exceptions exc1
exc2 ... that can be propagated from the method to the caller.
For exc1 exc2 ... , all exception classes that are visible at
this point that are subclasses of CX_STATIC_CHECK or
CX_DYNAMIC_CHECK can be specified here. The exception classes must be
specified in ascending order with respect to their inheritance
hierarchy. Each exception class may only be specified once.
The RESUMABLE addition declares an exception that can be
propagated as a resumable exception
. This means:
A resumable exception is propagated as a resumable exception.
The addition does not have any effect on a non-resumable exception.
If a resumable exception is propagated with RAISING without the
addition RESUMABLE , it thus becomes non-resumable.
If a superclass is declared as resumable, any subclasses must also be
declared as resumable.
Exceptions in the categories CX_STATIC_CHECK and
CX_DYNAMIC_CHECK must be explicitly declared, otherwise a propagation
can lead to an interface violation. A violation of the interface raises
the handleable exception CX_SY_NO_HANDLER . Exceptions of the
category CX_NO_CHECK are always declared implicitly and with the
RESUMABLE addition.
Latest notes: The declaration of exceptions of the category
CX_STATIC_CHECK is checked statically in the syntax check. For
exceptions of the category CX_DYNAMIC_CHECK , the check is not
performed until runtime.
In a method in which class-based exceptions are declared using the
addition RAISING , the statement CATCH
SYSTEM-EXCEPTIONS cannot be used. Instead, the relevant handleable
exceptions should be handled in a TRY control
structure.
An exception that is raised as resumable in the method with
RAISE RESUMABLE EXCEPTION ,
should also be declared as resumable in the interface, since the
exception would otherwise lose this property when the method is exited.
Example ABAP Coding In the class math , any exceptions represented by
the class CX_SY_ARITHMETIC_ERROR and its subclasses are
propagated from within the method divide_1_by . If, for example,
the input parameter operand is filled by the call with the value
0, then the exception CX_SY_ZERODIVIDE is raised, propagated, and
can, as shown in the example, be handled by the caller in a TRY
control structure.
CLASS math DEFINITION.
PUBLIC SECTION.
METHODS divide_1_by
IMPORTING operand TYPE i
RETURNING value(result) TYPE decfloat34
RAISING cx_sy_arithmetic_error.
ENDCLASS.
CLASS math IMPLEMENTATION.
METHOD divide_1_by.
result = 1 / operand.
ENDMETHOD.
ENDCLASS.
DATA oref TYPE REF TO math.
DATA exc TYPE REF TO cx_sy_arithmetic_error.
DATA res TYPE decfloat34.
DATA text TYPE string.
START-OF-SELECTION.
CREATE OBJECT oref.
TRY.
res = oref->divide_1_by( 4 ).
text = res.
CATCH cx_sy_arithmetic_error INTO exc.
text = exc->get_text( ).
ENDTRY.
MESSAGE text TYPE 'I'.
ABAP_ADDITION_5 ... EXCEPTIONS exc1 exc2 ...
What does it do? Use the addition EXCEPTIONS to define a list of
non-class-based exceptions exc1
exc2... that can be raised by the statements
RAISE or
MESSAGE RAISING in the method. The names exc1 exc2 ...
for the exceptions are freely definable and specified directly.
Exceptions defined in this way are bound to the method (similar to
formal parameters) and cannot be propagated.
If this type of exception is raised in a method and no return code has
been assigned to it in the addition EXCEPTIONS in the
method call , then a runtime error occurs.
The additions RAISING and EXCEPTIONS cannot be used
simultaneously. As before, in a method whose interface defines
non-class-based exceptions, the statement
RAISE EXCEPTION or the addition
THROW in a
conditional expression cannot be
used to raise class-based exceptions.
Latest notes: For new developments, we recommend that class-based
exceptions are used that are independent of the method in question.
Example ABAP Coding In the class math , for method divide_1_by ,
an exception arith_error is defined which is raised in the method
by the RAISE statement if an arithmetic error occurs. If, for
example, the input parameter operand is filled with value 0 by
the call, the exception arith_error is raised in the internal
method handling of exception CX_SY_ZERODIVIDE and handled after
the call of the method by evaluating sy-subrc . The method cannot
be called functionally due to the handling of the classical exception.
CLASS math DEFINITION.
PUBLIC SECTION.
METHODS divide_1_by
IMPORTING operand TYPE i
RETURNING VALUE(result) TYPE decfloat34
EXCEPTIONS arith_error.
ENDCLASS.
CLASS math IMPLEMENTATION.
METHOD divide_1_by.
TRY.
result = 1 / operand.
CATCH cx_sy_arithmetic_error.
RAISE arith_error.
ENDTRY.
ENDMETHOD.
ENDCLASS.
DATA res TYPE decfloat34.
DATA oref TYPE REF TO math.
START-OF-SELECTION.
CREATE OBJECT oref.
oref->divide_1_by( EXPORTING operand = 4
RECEIVING result = res
EXCEPTIONS arith_error = 4 ).
IF sy-subrc = 0.
cl_demo_output=>display_data( res ).
ELSE.
cl_demo_output=>display_text( 'Arithmetic error!' ).
ENDIF.
ABAP_ADDITION_6 ... ABSTRACT ...
What does it do? Use the addition ABSTRACT to define an abstract
method meth . The addition ABSTRACT is allowed only in
abstract classes, not in interfaces. An abstract method is not
implemented in the implementation section of its class. To implement an
abstract method, it must be redefined in a
specific subclass using addition REDEFINITION .
Latest notes: Abstract methods can be defined in classes that are either
abstract or final, but they can never be implemented and therefore are
not usable.
Methods in interfaces are implicitly abstract, because interfaces do
not contain method implementations.
With the exception of the instance constructor, concrete instance
methods of a class can also call their abstract methods.
Static methods cannot be redefined and the addition ABSTRACT is
not allowed in their declarations.
ABAP_ADDITION_7 ... FINAL ...
What does it do? The addition FINAL is allowed only in classes, not
in interfaces. Use the addition FINAL to define a final method
meth . A final method cannot be redefined in a subclass. In final
classes, all methods are automatically final; the addition FINAL
is not allowed.
Latest notes: Static methods cannot be redefined and the addition
FINAL is not allowed in their declarations.
Documentation extract taken from SAP system, � Copyright SAP AG. All rights reserved