10

Click here to load reader

ERPtips SAP Training Manual SAMPLE CHAPTER From ABAP Objects

Embed Size (px)

DESCRIPTION

SAP Training

Citation preview

Page 1: ERPtips SAP Training Manual SAMPLE CHAPTER From ABAP Objects

Object Oriented ABAP ECC 6.0 Rev. 10/15/08 1 SAPtips Sample Object Oriented ABAP Exceptions

This segment from our 265 page Object Oriented ABAP manual is being made available as a sample of our training manuals. Please contact [email protected] if you have any SAP training needs, or visit www.ERPtips.com for more information.

Written by John Verbestel

Published by Klee Associates, Inc.

Copyright Klee Associates, Inc., 2008

Unit 3 – Specialized Classes

Exceptions

Exception refers to a situation where an ABAP processing block, such as a

Function Module or Method, can no longer continue.

Some examples are: a divide by zero situation, field overflow, generic casting

error.

The new exception concept is comprised of the following elements:

Exceptions classes

ABAP Syntax elements TRY/ENDTRY and CATCH.

The standard exception process uses the system field SUBRC set to a non-zero

number if an operation, such as SELECT or LOOP AT, fails, or a function module

Exceptions is triggered using the RAISE exception.

When a method is created, the exceptions can either be based on an exception

class (Class Based Exceptions) or the standard exception process, much like

function modules.

Class-based exceptions can be based on message classes or directly-defined

textual exceptions.

Exception classes are defined with the prefix CX. For customer exception

classes, define the class as ZCX_<ExceptionClass>.

Page 2: ERPtips SAP Training Manual SAMPLE CHAPTER From ABAP Objects

Unit 3 – Specialized Classes Klee Associates, Inc.

2 Rev. 10/15/08 Object Oriented ABAP ECC 6.0

Handling Exceptions

The first CATCH keyword starts the CATCH block. All previous coding is

controlled under the exception handling of the TRY/ENDTRY construct.

TRY/ENDTRY blocks can be nested, and the CLEANUP handles passing

exceptions to a higher-up TRY/ENDTRY construct.

Page 3: ERPtips SAP Training Manual SAMPLE CHAPTER From ABAP Objects

Klee Associates, Inc. Unit 3 – Specialized Classes

Object Oriented ABAP ECC 6.0 Rev. 10/15/08 3

Predefined Exceptions

The SAP software groups the predefined exceptions as three specialization

branches under CX_ROOT as follows:

CX_no_check

CX_dynamic_check

CX_static_check

The IF_Message interface provides the get_text method that is used to retrieve

the error message based on the textid. Messages provided by an exception

class can be stored in a data object of type string.

Several texts can be assigned to each class. The textid is used in the RAISE

EXCEPTION statement to supply the appropriate message based on the error at

hand.

The Kernel_errid attribute holds the SAP Software system runtime error code

that was raised, and is only pertinent in the case of SAP pre-defined exceptions.

Under CX_DYNAMIC_CHECK, you will find the exception classes for Arithmetic

errors, field value overflows, and the like.

Page 4: ERPtips SAP Training Manual SAMPLE CHAPTER From ABAP Objects

Unit 3 – Specialized Classes Klee Associates, Inc.

4 Rev. 10/15/08 Object Oriented ABAP ECC 6.0

Subclasses of Standard Exception Classes

The CX_DYNAMIC_CHECK, CX_STATIC_CHECK, and CX_NO_CHECK can be

viewed using the ABAP Repository Browser (SAP Transaction SE80). This can be

used to review the exception class hierarchies.

The Subclasses can be viewed using the Object Navigator as demonstrated

in the following figures.

Page 5: ERPtips SAP Training Manual SAMPLE CHAPTER From ABAP Objects

Klee Associates, Inc. Unit 3 – Specialized Classes

Object Oriented ABAP ECC 6.0 Rev. 10/15/08 5

Exception Class Creation

When creating exception classes, Message Class supports imply that exception

messages are provided by Message Classes created via SAP Transaction SE91.

Each message class supports 999 language dependent texts with up to four

variables in each message that can be replaced at runtime with runtime values.

Message Classes also support long texts that can provide additional information

to the user.

The following steps present how to create exception classes.

1. Create an Exception class, using SAP Transaction SE24, the Class Builder.

Page 6: ERPtips SAP Training Manual SAMPLE CHAPTER From ABAP Objects

Unit 3 – Specialized Classes Klee Associates, Inc.

6 Rev. 10/15/08 Object Oriented ABAP ECC 6.0

Exception classes can be created either with or without a Message Class

support. Custom exception classes begin with a ZCX_ prefix. The

standard superclass is CX_STATIC_CHECK.

The created exception class has standard methods, interfaces, and aliases

required to support exception handling.

The same methods and aliases are defined regardless of the exception class

type (with/without message class).

The following describes the standard methods provided:

GET_SOURCE_POSITION method returns the name of the main program and (if relevant) the names of the include program and the line number in the source code where the exception occurred.

The GET_TEXT method returns an exception text in the form of a string.

Page 7: ERPtips SAP Training Manual SAMPLE CHAPTER From ABAP Objects

Klee Associates, Inc. Unit 3 – Specialized Classes

Object Oriented ABAP ECC 6.0 Rev. 10/15/08 7

The GET_LONGTEXT returns the long description of the message, based on message class long texts.

The Aliases can be used for the methods from the interface IF_MESSAGE,

instead of having to type in the complete method name.

2. Create Exception Texts

Exception Classes without Message Class support

For methods without message class support, in the Texts tab, an

exception ID is entered and the text is defined directly.

Texts are entered directly for each message ID. Message IDs can be

added and don’t need to conform to any particular naming convention.

Exception Classes with Message Class Support

For messages with Message Class support, in the Texts tab, enter in the

exception name and click on the change message button.

Page 8: ERPtips SAP Training Manual SAMPLE CHAPTER From ABAP Objects

Unit 3 – Specialized Classes Klee Associates, Inc.

8 Rev. 10/15/08 Object Oriented ABAP ECC 6.0

The prompt then allows entry of the message class/number and

message parameters.

Each attribute replaces the & in the message text, numbered from left

to right, 1 through 4. Attributes are defined in the Attributes tab of the

exception class. Attributes cannot be complex objects or functional

methods. The attributes would need to be supplied by the program

raising the exception. The EXPORTING addition of the RAISE EXCEPTION

is required.

Standard attributes for messages without message class support are displayed

below:

Page 9: ERPtips SAP Training Manual SAMPLE CHAPTER From ABAP Objects

Klee Associates, Inc. Unit 3 – Specialized Classes

Object Oriented ABAP ECC 6.0 Rev. 10/15/08 9

Standard attributes for messages with message class support are displayed

below.

Additional attributes are added for message parameters, and should be

public

A constant attribute exists for each exception id that is created under the

Texts tab. This is used by the RAISE EXCEPTION TYPE <exception class>

EXPORTING TEXTID = ExceptionClass=>exceptionidconstant

Legal Speak:

Purchase of this book or acquisition at an ERPtips class, constitutes an implied

agreement by the purchaser to a limited, license granted to the purchaser by

Klee Associates, Inc., specifically for purposes limited to improving your personal

knowledge of SAP software which does not in any way compete with Klee

Associates.

This manual is protected by copyright law, and is sold or is part of an ERPtips

class with the following conditions:

The manual will be used for your personal professional development.

The manual will not be physically copied for distribution to other individuals

or converted to an electronic copy for distribution to other individuals, unless

copyright permissions for copying have been acquired from Klee Associates,

Inc.

Page 10: ERPtips SAP Training Manual SAMPLE CHAPTER From ABAP Objects

Unit 3 – Specialized Classes Klee Associates, Inc.

10 Rev. 10/15/08 Object Oriented ABAP ECC 6.0

The manual will not be used to conduct training classes by you or other

individuals, without the written permission of Klee Associates, Inc.

Inclusion of the concepts in this book in any internal client training material is

allowed if the source of the concepts is acknowledged as follows:

Copyright 2008

All rights reserved.

Used with permission.

Klee Associates, Inc.

NO WARRANTY: This documentation is delivered as is, and Klee Associates

makes no warranty as to its accuracy or use. Any use of this documentation is

at the risk of the user. Although we make every good faith effort to ensure

accuracy, this document may include technical or other inaccuracies or

typographical errors. Klee Associates reserves the right to make changes

without prior notice.

ERPtips is a valued resource for thousands of clients and consultants worldwide.

Visit our web site for more information about upcoming training, books, and

newsletters.

www.ERPtips.com

ERPtips is a registered trademark of Klee Associates, Inc.

No Affiliation: Klee Associates, Inc. and this publication are not affiliated with or

endorsed by SAP AG. SAP AG software referred to on this site is furnished under

license agreements between SAP AG and its customers and can be used only

within the terms of such agreements. SAP AG is a registered trademark of SAP

AG. All other product names used herein are trademarks or registered

trademarks of their respective owners.