Sie sind auf Seite 1von 14

IBM Global Business Services

ABAP Objects Advanced Class


Based Exception Handling
IBM Corporation 2013

IBM Global Business Services

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

Introduction & Overview

July-2007

IBM Corporation 2013

IBM Global Business Services

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.

Introduction & Overview

July-2007

IBM Corporation 2013

IBM Global Business Services

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?

Introduction & Overview

July-2007

IBM Corporation 2013

IBM Global Business Services

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.

Example Code

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

Introduction & Overview

July-2007

IBM Corporation 2013

IBM Global Business Services

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.

Introduction & Overview

July-2007

IBM Corporation 2013

IBM Global Business Services

Benefits of class based exceptions


Class based exceptions improve upon classical exception handling
Grouping of exceptions is facilitated by inheritance
Exceptions dont always have to be handled immediately. If an exception
cant 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.

Introduction & Overview

July-2007

IBM Corporation 2013

IBM Global Business Services

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


8

Introduction & Overview

July-2007

IBM Corporation 2013

IBM Global Business Services

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 cant be mixed in
routines!

Introduction & Overview

July-2007

IBM Corporation 2013

IBM Global Business Services

Handling Class-Based Exception(Structure)

10

Introduction & Overview

July-2007

IBM Corporation 2013

IBM Global Business Services

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
11

Introduction & Overview

July-2007

IBM Corporation 2013

IBM Global Business Services

Demonstration
Handling Class based exceptions:

Example Code

12

Introduction & Overview

July-2007

IBM Corporation 2013

IBM Global Business Services

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

13

Introduction & Overview

July-2007

IBM Corporation 2013

IBM Global Business Services

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

Example Code

14

Introduction & Overview

July-2007

IBM Corporation 2013

Das könnte Ihnen auch gefallen