abap objects - defining a class-based exceptions

4
ABAP Objects - Defining a Class-based exceptions Class-based Exceptions Definition *----------------------------------------------------------------------* * CLASS class_exception DEFINITION *----------------------------------------------------------------------* * All Exception Class must inherit from Class CX_ROOT or its subclass *----------------------------------------------------------------------* CLASS class_exception DEFINITION INHERITING FROM cx_static_check. PUBLIC SECTION. METHODS write_msg. ENDCLASS. "CX_SAMPLE_EXCEPTION DEFINITION Class-based Exceptions Implementation *----------------------------------------------------------------------* * CLASS class_exception IMPLEMENTATION *----------------------------------------------------------------------* CLASS class_exception IMPLEMENTATION. METHOD write_msg. WRITE / 'Method of Class class_exception'. ENDMETHOD. ": ENDCLASS. "class_exception IMPLEMENTATION Class main Definition *----------------------------------------------------------------------* * CLASS main DEFINITION *----------------------------------------------------------------------* CLASS main DEFINITION. PUBLIC SECTION. * This method use the class exceptions class_exception * to deal with error. To do this we use the statement RAISING METHODS action RAISING class_exception. ENDCLASS. "main DEFINITION Class main Implementation *----------------------------------------------------------------------* * CLASS main IMPLEMENTATION

Upload: writeme670

Post on 13-Apr-2016

220 views

Category:

Documents


0 download

DESCRIPTION

ABAP Objects - Defining a Class-based Exceptions

TRANSCRIPT

Page 1: ABAP Objects - Defining a Class-based Exceptions

ABAP Objects - Defining a Class-based exceptions

Class-based Exceptions Definition

*----------------------------------------------------------------------** CLASS class_exception DEFINITION*----------------------------------------------------------------------** All Exception Class must inherit from Class CX_ROOT or its subclass*----------------------------------------------------------------------*CLASS class_exception DEFINITION INHERITING FROM cx_static_check. PUBLIC SECTION. METHODS write_msg. ENDCLASS. "CX_SAMPLE_EXCEPTION DEFINITION

Class-based Exceptions Implementation

*----------------------------------------------------------------------** CLASS class_exception IMPLEMENTATION*----------------------------------------------------------------------*CLASS class_exception IMPLEMENTATION. METHOD write_msg. WRITE / 'Method of Class class_exception'. ENDMETHOD. ":ENDCLASS. "class_exception IMPLEMENTATION

Class main Definition

*----------------------------------------------------------------------** CLASS main DEFINITION*----------------------------------------------------------------------*CLASS main DEFINITION. PUBLIC SECTION.* This method use the class exceptions class_exception* to deal with error. To do this we use the statement RAISING METHODS action RAISING class_exception. ENDCLASS. "main DEFINITION

Class main Implementation

*----------------------------------------------------------------------** CLASS main IMPLEMENTATION

Page 2: ABAP Objects - Defining a Class-based Exceptions

*----------------------------------------------------------------------*CLASS main IMPLEMENTATION. METHOD action. * Here we're raising an exception that should be Treated by* exception class class_exception RAISE EXCEPTION TYPE class_exception. ENDMETHOD. "actionENDCLASS. "main IMPLEMENTATION

Defining Objects

DATA o_exception TYPE REF TO class_exception.DATA o_main TYPE REF TO main.

Instance Creation

START-OF-SELECTION. CREATE OBJECT o_main.

Calling Methods* The statement TRY must be used to define a block that CATCH the exceptions TRY. o_main->action( ). * The Statement CATCH define a block that catches the exceptions of the* exception class class_exception CATCH class_exception. WRITE / 'Exception Caught'.* Or just use this MESSAGE 'Exception caught' TYPE 'I'. ENDTRY. * The statement TRY must be used to define a block that CATCH the exceptions TRY. o_main->action( ). * The Statement CATCH define a block that catches the exceptions of the* exception class class_exception CATCH class_exception INTO o_exception. MESSAGE 'Exception caught' TYPE 'I'. * Here we're calling the method of class class_exception o_exception->write_msg( ). ENDTRY.

----------------

Page 3: ABAP Objects - Defining a Class-based Exceptions

ABAP Objects Sample code - Exception Handling - Class based exceptions

REPORT z_exception_handle. *" Class cls definition------------------------------------------------*CLASS cls DEFINITION. PUBLIC SECTION. METHODS: mult IMPORTING im_var1 TYPE i im_var2 TYPE i RAISING cx_sy_arithmetic_overflow, display. PRIVATE SECTION. DATA: w_var1 TYPE i, w_var2 TYPE i, w_result TYPE i .ENDCLASS. "cls DEFINITION*" Class cls implementation--------------------------------------------*CLASS cls IMPLEMENTATION. METHOD mult. w_var1 = im_var1. w_var2 = im_var2. w_result = im_var1 * im_var2. ENDMETHOD. " mult, the Multiplication method METHOD display. WRITE: / w_var1,'*',w_var2,'=',w_result. ENDMETHOD. "display, Display the resultENDCLASS. "cls IMPLEMENTATION*" Data declarationPARAMETERS: p_var1 TYPE i, p_var2 TYPE i.DATA: ref TYPE REF TO cls, ref_var TYPE REF TO cx_sy_arithmetic_overflow, text TYPE string.START-OF-SELECTION. CREATE OBJECT ref. CREATE OBJECT ref_var. TRY. ref->mult( im_var1 = p_var1 im_var2 = p_var2 ). ref->display( ). CATCH cx_sy_arithmetic_overflow INTO ref_var. text = ref_var->get_text( ). MESSAGE text TYPE 'I'.

Page 4: ABAP Objects - Defining a Class-based Exceptions

ENDTRY. " TRY block