07 abap objects - class based exceptions

14
© IBM Corporation 2013 IBM Global Business Services ABAP Objects Advanced – Class Based Exception Handling

Upload: bakkalibilal

Post on 18-Jan-2016

38 views

Category:

Documents


4 download

DESCRIPTION

07 ABAP Objects - Class Based Exceptions

TRANSCRIPT

Page 1: 07 ABAP Objects - Class Based Exceptions

© IBM Corporation 2013

IBM Global Business Services

ABAP Objects Advanced – Class Based Exception Handling

Page 2: 07 ABAP Objects - Class Based Exceptions

IBM Global Business Services

© IBM Corporation 20132 July-2007Introduction & Overview

Objectives

Overview

What is wrong with “Classical” exception handling

Treatable Exceptions in Release 6.10 and later

Benefits of class based exceptions

Class-Based Exceptions

Exception Classes

Triggering Class-Based Exceptions

Handling Class-Based Exceptions

Creating a global exception class

Page 3: 07 ABAP Objects - Class Based Exceptions

IBM Global Business Services

© IBM Corporation 20133 July-2007Introduction & Overview

What are Exceptions?

Exceptions are responses to situations during execution of an ABAP Program in which the normal continuation of the program is not possible or does not make sense.

Exception situations can be detected by either the program or the Runtime Environment.

When the ABAP program or the Run time environment detects an exception situation,an exception is triggered.

Page 4: 07 ABAP Objects - Class Based Exceptions

IBM Global Business Services

© IBM Corporation 20134 July-2007Introduction & Overview

Why a new Class Based Exception Concept?

There is already a way to handle exceptions in ABAP via the RAISE statement and the EXCEPTIONS clause in function modules and methods.

So, Why is there a need for a new exception concept?

Page 5: 07 ABAP Objects - Class Based Exceptions

IBM Global Business Services

© IBM Corporation 20135 July-2007Introduction & Overview

Current way of handling exceptions: Problems

This slide is meant to get the class thinking about the current way in which exceptions are handled. The following example can be used to refresh the memory on current exception handling.

All exceptions must be handled immediately after the function call, which is cumbersome.

There is no way to group similar exceptions, except through the use of “others”.

Exceptions returned from classical exceptions are merely return codes and affect no flow control processing

Example Code

Page 6: 07 ABAP Objects - Class Based Exceptions

IBM Global Business Services

© IBM Corporation 20136 July-2007Introduction & Overview

Treatable Exceptions in Release 6.10 and later

Class based exceptions are available. They are instances of exception classes, which are either predefined globally in the system or user defined globally or locally.

All known class based exceptions can be triggered with RAISE EXCEPTION and handled with TRY control structures.

If no handler is found, a runtime error will occur

SAP provides numerous classes designated as exception classes which can be used in ABAP objects, such as programs, function modules, classes, etc.

If an exception does not exist to handle a particular situation, it can be created, using transaction SE24 (Class Builder).

An exception can be automatically triggered by the system, for example, an arithmetic overflow or it can be triggered manually using the RAISE EXCEPTION command.

Page 7: 07 ABAP Objects - Class Based Exceptions

IBM Global Business Services

© IBM Corporation 20137 July-2007Introduction & Overview

Benefits of class based exceptions

Class based exceptions improve upon classical exception handling

Grouping of exceptions is facilitated by inheritance

Exceptions don’t always have to be handled immediately. If an exception can’t be handled immediately, the exception can be propagated along the call chain until a suitable handler is found.

Flow control is possible upon catching of exceptions and object attributes store data about the exception context for use down the line

Inheritance enables refinement of exceptions. That is, by adding new attributes, you can reuse existing exceptions by making them more specific.

Page 8: 07 ABAP Objects - Class Based Exceptions

IBM Global Business Services

© IBM Corporation 20138 July-2007Introduction & Overview

Exception Classes

Exception classes are subclasses of the Global class:

CX_STATIC_CHECK

Exceptions defined through One of the sub classes of CX_STATIC_CHECK have to be either handled within a procedure or passed on explicitly with the addition RAISING.

However this is verified by the syntax check.

CX_DYNAMIC_CHECK

Exceptions defined through One of the sub classes of CX_DYNAMIC_CHECK have to be either handled within a procedure or passed on explicitly with the addition RAISING.

This is not verified by syntax check and the check is postponed until an exception occurs.

CX_NO_CHECK

Exceptions defined through One of the sub classes of CX_NO_CHECK can be either handled within a procedure or passed on implicitly.

We should not use Class CX_NO_CHECK or its subclasses explicitly in the RAISING addition, as they are already contained implicitly.

Common Super Class is CX_ROOT

Page 9: 07 ABAP Objects - Class Based Exceptions

IBM Global Business Services

© IBM Corporation 20139 July-2007Introduction & Overview

Triggering Class-Based Exceptions

RAISE EXCEPTION TYPE cx_class EXPORTING pi = ai

cx_class - Class-based exception

pi = ai assigns actual parameters to instance parameters

NOTE: The old and new ways of handling exceptions can’t be mixed in routines!

Page 10: 07 ABAP Objects - Class Based Exceptions

IBM Global Business Services

© IBM Corporation 201310 July-2007Introduction & Overview

Handling Class-Based Exception(Structure)

Page 11: 07 ABAP Objects - Class Based Exceptions

IBM Global Business Services

© IBM Corporation 201311 July-2007Introduction & Overview

Handling Class-Based Exceptions

TRY

try block - Normal executable code is executed up to the CATCH. If no exceptions occurred,

processing continues after ENDTRY. If an exception occurs which is handled by the

CATCH, control passes to the CATCH block

CATCH cx_class INTO oref

CATCH block is an exception handler. cx_class is the class-based exception being handled

by the CATCH block. Exception classes should be listed starting with the most specific to

the most general

INTO oref captures the reference to the exception object. Oref must be an object reference

variable with suitable static type. Oref can be used to access the attributes and methods of

the exception object

CLEANUP

Clean up block is executed whenever an exception occurs within the TRY block and is not

handled by a CATCH within the same TRY block, BUT is handled by an surrounding TRY

block

ENDTRY

Page 12: 07 ABAP Objects - Class Based Exceptions

IBM Global Business Services

© IBM Corporation 201312 July-2007Introduction & Overview

Demonstration

Handling Class based exceptions:

Example Code

Page 13: 07 ABAP Objects - Class Based Exceptions

IBM Global Business Services

© IBM Corporation 201313 July-2007Introduction & Overview

Demonstration

Propagation of Class-Based exceptions in procedures to the caller.

Class-based exceptions in procedures can be propagated to the caller in the definition of the interface using the RAISING addition, if the exception is not to be handled in the procedure.

Example Code

Page 14: 07 ABAP Objects - Class Based Exceptions

IBM Global Business Services

© IBM Corporation 201314 July-2007Introduction & Overview

Demonstration

Detailed information about an exception ca be obtained from objects that are created from exception classes when error is trapped.

Example Code