METHODS - IMPORT, EXPORT, MODIFY, INCREASE (2023)

SAP NetWeaver AS ABAP version 752, ©Copyright 2017 SAP AG. All rights reserved.

ABAP - Keyword Documentation→ABAP - Reference→Declarations→Declarations→Classes and Interfaces→Components in Classes and Interfaces→Methods→METHODS→

METHODS - IMPORT, EXPORT, MODIFY, INCREASE

quick View

Syntax

METHODS Methamphetamine[SUMMARY|CONCLUSION]
|[IGNORE DEFAULT|ERROR]

[IMPORT parameters[PREFERRED PARAMETER p]]
[export parameters]
[CHANGE parameters]
[{ASCENSOR V1|RECOVERY(exc1) exc2|RESUME (exc2) ...}
|{EXCEPTION excellent1 excellent2...}].

Extras:

1. ... parameter IMPORT [PREFFERENT PARAMETER p]

2. ...EXPORT parameters

3. ... change parameters

4. ... UP exc1|CONTINUE(exc1) exc2|CONTINUE(exc2) ...

5. ... EXCEPTIONS excellent1 excellent2 ...

Effect

This declaration declares a general instance methodmethamphetamine. With the additions ABSTRACT and FINAL, the method of a class can be made abstract or final. The DEFAULT addition is used to make an interface method optional.

the additionsTO IMPORT,TO EXPORT, yCHANGEDefine the parameter interface of the method. After each addition, the corresponding formal parameters are defined by specifying a list parameter. The order of additions is fixed.

The remaining additions determine which exceptions the method throws or throws.

Use

Within a method, the IS SUPPLIED predicate expression can be used to check whether an actual parameter was assigned to an optional formal parameter when it was called.

supplement 1

... IMPORT parameters[PREFERRED PARAMETER p]

Effect

TO IMPORTdefines the input parameters. When the method is called, a suitable actual parameter must be specified for each non-optional input parameter. The content of the current parameter is passed to the input parameter when it is called. The content of an input parameter that is defined as pass-by-reference cannot be changed in the method.

PREFERRED PARAMETERcan be used to identify an input parameterp1p2...from the Parameters by list.TO IMPORTas the preferred parameter. This can only be specified if all input parameters and input/output parameters are optional. The parameter specified afterPREFERRED PARAMETERis implicitly set to optional. When the method is called with the syntaxmetanfetamina (a)(as standalone or functional syntax), the actual parameteramaps to the preferred input parameterpag.

Use

despitePREFERRED PARAMETERdoes the parameterpagimplicit optional, this parameter must be made explicit with optionalOPTIONALoORIGINALLY. Otherwise, a syntax check warning occurs.

example

Declaration of a method with only optional input parameters, one of which is marked as the preferred parameter.

CLASE Cls DEFINITION.
PUBLIC PART.
METHODS
methamphetamine
TO IMPORT
p1 TYPICAL i STANDARD 111
p2 TYPICAL i OPTIONAL
p3 TYPICAL i OPTIONAL
PREFERRED PARAMETER p1.
...
END OF CLASS.

supplement 2

... EXPORT parameters

Effect

TO EXPORTdefines the output parameters. When the method is called, a suitable actual parameter can be specified for each output parameter. The content of an output parameter defined to be passed by value is passed to the actual parameter after the method has completed successfully.

Use

An output parameter defined to be passed by reference behaves like an input/output parameter, that is, it is not initialized when the method is called. For this reason, it should not be read before writing for the first time. Also, care must be taken when adding content to such parameters, for example when inserting rows into internal tables.

example

The methodread_spfli_into_tableof this example has one input and one output parameter that are written entirely by referencing the ABAP Dictionary.

DEFINITION OF CLASS FLIGHTS.
PUBLIC PART.
MÉTODO read_spfli_into_table
IMPORT VALUE(id)TYPE spfli-carrid
EXPORTAR flight_tab TIPO spfli_tab.
...
END OF CLASS.

supplement 3

... CHANGING parameters

Effect

CHANGEdefines the input/output parameters. When the method is called, a suitable actual parameter must be specified for each non-optional input/output parameter. The content of the actual parameter is passed to the input/output parameter when it is called. After the method has finished, the content of the input/output parameter is transferred to the current parameter.

example

Declaration of a method with input and input/output parameters.

HTML CLASS DEFINITION.
PUBLIC PART.
TYPEN html_table TYPE...
...
METHODS
add_text_to_html
TO IMPORT
text TYPE-String
CHANGE
html TYPE html_table.
...
END OF CLASS.

supplement 4

... ELEVATION V1|RECOVERY(exc1) exc2|RESUME (exc2) ...

Effect

The additionRAISEused to declare class-based exceptionsV1 V2...that can be passed from the method to the caller.

ForV1 V2...all exception classes visible at this point that are subclasses of CX_STATIC_CHECK or CX_DYNAMIC_CHECK can be specified here. Exception classes must be specified in ascending order with respect to their inheritance hierarchy. Each exception class can only be specified once.

The additionRESUMEdeclares an exception that can be propagated as a resumable exception. This means:

  • A resumable exception is propagated as a resumable exception.
  • Unprocessable exceptions ignore addition.
  • If a resumable exception is propagated inRAISEand the additionRESUMEis not specified, therefore it becomes non-continuable.

When a superclass is declared resumable, all subclasses must also be declared resumable.

Exceptions in the CX_STATIC_CHECK and CX_DYNAMIC_CHECK categories must be declared explicitly to prevent propagations from violating the interface. Interface violations trigger the CX_SY_NO_HANDLER handleable exception. Exceptions of the CX_NO_CHECK category are always declared implicitly and with the additionRESUME.

Observations

  • Exception declarations in the CX_STATIC_CHECK category are checked statically during syntax checking. For exceptions in the CX_DYNAMIC_CHECK category, the check is only done at runtime.
  • In a method where class-based exceptions are declared with the additionRAISE,The deprecated CATCH SYSTEM-EXCEPTIONS statement cannot be used to handle catchable runtime errors. Instead, handleable exceptions associated with runtime errors must be handled in a TRY control structure.
  • An exception that is raised in the method with RAISE REANUABLE EXCEPTION as resumable must also be declared as resumable on the interface; otherwise the exception would lose this property when the method is exited.
  • Also see Class-Based Exceptions in Procedures.

example

In the classmathematics, all exceptions represented by the CX_SY_ARITHMETIC_ERROR class and its subclasses are propagated inside the methoddivide_1_by. For example, if the input parameteroperatingis filled with the value 0 by the call, the CX_SY_ZERODIVIDE exception is generated, propagated, and can be handled by the caller in oneTO TRYControl structure 8 (as shown in the example).

DEFINITION OF CLASS MATHEMATICS.
PUBLIC PART.
METHODS divide_1_by
IMPORT-Operating TYP i
RETURN value (result) TYPE decfloat34
LEVANTANDO cx_sy_arithmetic_error.
END OF CLASS.

IMPLEMENTATION OF MATH CLASSES.
METHOD divide_1_by.
Result = 1 / operand.
END METHOD.
END OF CLASS.

DATA oref TYPE REF ON maths.
DATOS excTYPE REF TO cx_sy_arithmetic_error.
DATA resTYPE decfloat34.
DATA text TYPE String.

START OF SELECTION.

CREATE OBJECT oref.
TO TRY.
res = oref->divide_1_by( 4 ).
text = res.
CAPTURA cx_sy_arithmetic_error EN exc.
texto = exc->get_text().
PROHIBITED.
TEXT MESSAGE TYPE 'I'.

supplement 5

...AUSNAHMEN V1 V2 ...

Effect

The additionEXCEPTIONSused to define a list of non-class based exceptionsexcellent1 excellent2...which can be triggered by the RAISE or MESSAGE RAISING statements in the method. The namesV1 V2...because exceptions can be freely defined and are specified directly. Exceptions defined in this way are bound to the method (similar to formal parameters) and cannot be propagated.

If such an exception is thrown in a method and no return value has been assigned to it on additionEXCEPTIONSA runtime error occurs when the method is called.

the additionsRAISEyEXCEPTIONScannot be used at the same time. In a method whose interface defines non-class exceptions, the RAISEEXCEPTION statement or the THROW addition in a conditional expression cannot be used to raise class-based exceptions.

Use

For new developments, it is recommended to use exceptions based on classes that are independent of the respective method.

example

In the classmathematics, a non-class-based exceptionerror_aritmois defined for the methoddivide_1_bythat is triggered in the method by the statementINCREASEwhen a calculation error occurs. For example, if the input parameteroperatingis filled with the value 0 by the call, the exceptionerror_aritmois triggered in the inner method's handling of the CX_SY_ZERODIVIDE exception and is evaluated after the method is calledy subrc. The method cannot be called functionally due to classic exception handling.

DEFINITION OF CLASS MATHEMATICS.
PUBLIC PART.
METHODS divide_1_by
IMPORT-Operating TYP i
RETURN VALUE (Result) TYPE decfloat34
EXCEPTION error_arith.
END OF CLASS.

IMPLEMENTATION OF MATH CLASSES.
METHOD divide_1_by.
TO TRY.
Result = 1 / operand.
CATCH cx_sy_arithmetic_error.
RAISE arit_error.
PROHIBITED.
END METHOD.
END OF CLASS.

DATA resTYPE decfloat34.
DATA oref TYPE REF ON maths.

START OF SELECTION.

CREATE OBJECT oref.
oref->divide_1_by(EXPORTARoperando = 4
RECEIVE result = res
EXCEPTION arith_error = 4 ).

IF and subrc = 0.
cl_demo_output=>display_data(res).
ANDERS.
cl_demo_output=>display_text( 'Rechenfehler!' ).
IT WILL END YES.

Top Articles
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated: 02/08/2023

Views: 6455

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.