10-31-2007 8:53 AM
Hi all,
How to handle the user defined exception handling.
regards,
Rakesh.
10-31-2007 3:48 PM
Hi Rakesh,
Are you talking about class based exceptions?
If yes..
Class based exceptions will help us to avoid dumps.
Example, if we are diving something with ZERO it would go to a dump. So to avoid the same we will put the statement in TRY.. ENDTRY.. Block..
Below is the sample code for the same.
REPORT Y_EXCEPTION.
DATA: i TYPE i VALUE 1.
START-OF-SELECTION.
TRY.
i = i / 0.
CATCH cx_sy_zerodivide.
write:/5 'Divide by zero caught'.
ENDTRY.
If you want to create your own exceptions... Below code could help you.
REPORT Y_EXCEPTION.
CLASS CX_SAMPLE_EXCEPTION DEFINITION INHERITING FROM CX_STATIC_CHECK.
public section.
methods : meth1.
ENDCLASS.
CLASS CX_SAMPLE_EXCEPTION IMPLEMENTATION.
method : meth1.
write:/5 'I am a method in exception'.
endmethod.
ENDCLASS.
CLASS SOME_CLASS DEFINITION.
PUBLIC SECTION.
METHODS: m1 raising cx_sample_exception .
ENDCLASS.
CLASS SOME_CLASS IMPLEMENTATION.
METHOD m1.
RAISE EXCEPTION TYPE CX_SAMPLE_EXCEPTION.
ENDMETHOD.
ENDCLASS.
DATA: c1 TYPE REF TO SOME_CLASS.
START-OF-SELECTION.
TRY.
CREATE OBJECT c1.
c1->m1( ).
CATCH CX_sample_exception.
write:/5 'Exception caught'.
ENDTRY.
You can see standard example on se38, DEMO_HANDLE_EXCEPTIONS.
REPORT DEMO_HANDLE_EXCEPTIONS.
PARAMETERS NUMBER TYPE I.
DATA RESULT TYPE P DECIMALS 2.
DATA OREF TYPE REF TO CX_ROOT.
DATA TEXT TYPE STRING.
START-OF-SELECTION.
WRITE: / 'Testing divison and Sqare root with', NUMBER.
ULINE.
TRY.
IF ABS( NUMBER ) > 100.
RAISE EXCEPTION TYPE CX_DEMO_ABS_TOO_LARGE.
ENDIF.
TRY.
RESULT = 1 / NUMBER.
WRITE: / 'Result of division:', RESULT.
RESULT = SQRT( NUMBER ).
WRITE: / 'Result of square root:', RESULT.
CATCH CX_SY_ZERODIVIDE INTO OREF.
TEXT = OREF->GET_TEXT( ).
CLEANUP.
CLEAR RESULT.
ENDTRY.
CATCH CX_SY_ARITHMETIC_ERROR INTO OREF.
TEXT = OREF->GET_TEXT( ).
CATCH CX_ROOT INTO OREF.
TEXT = OREF->GET_TEXT( ).
ENDTRY.
IF NOT TEXT IS INITIAL.
WRITE / TEXT.
ENDIF.
WRITE: / 'Final result:', RESULT.
In the above example CX_DEMO_ABS_TOO_LARGE and CX_SY_ZERODIVIDE, CX_SY_ARITHMETIC_ERROR are standard class exceptions.
Regards,
Sreekanth
*<i>Do not forget to reward if it helps you...</i>
11-01-2007 12:53 PM
Hi,
We use the term exception to refer to a situation that arises while a program is
being executed, where there is no point in continuing to run the program in the
normal way. The SAP Web AS 6.10 introduced a new ABAP Objects exception
concept, parallel to the existing concept. Exceptions and exception handling are
now based on classes. This concept includes the concepts that preceded it but
also enhances (and thus replaces) them.
Class-based exceptions are raised either by the RAISE EXCEPTION statement
or by the runtime environment. Division by zero, for example, would be an
example of the latter.
In an exception situation, an exception is represented by an exception object -
that is, an instance of an exception class. The attributes of each exception object
contain information about the error situation.
When handling an exception, you can have it raise new exceptions and thus create
a chain of exceptions. Thus, in special cases, you can catch a runtime exception
and then have it raise an exception of its own.
You can define exception classes yourself, but there is already a range of
predefined exception classes in the system – particularly for exceptions in the
runtime environment. You usually create exception classes globally in the Class
Builder, but you can also define local exception classes within a program or
global class.
The use of class-based exceptions is not limited to object-oriented contexts.
Class-based exceptions can be raised and handled in all processing blocks.
In particular, all previously catchable runtime errors can now be handled as
class-based exceptions.
If a class-based exception is raised, the system interrupts the normal program flow
and tries to navigate to a suitable handler. If it cannot find a handler, a runtime
error occurs.
All exception classes are derived from the one of the classes CX_NO_CHECK,
CX_DYNAMIC_CHECK, or CX_STATIC_CHECK, themselves derived from
the common superclass CX_ROOT.
The way in which exception classes are assigned to one of these three paths in
the hierarchy defines how the associated exceptions are propagated. (This will be
discussed in more detail later in this unit.)
In the SAP standard system, the names of all exception classes start with CX_.
The CX_ROOT class provides some predefined methods that are inherited by all
exception classes: The 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.
Newly defined exception classes inherit from one of these superclasses, so that
other components can be added. These are usually individual exception texts.
You can assign several texts to each class. The IDs assigned to these are created
by the Class Builder as identically named static constants. You can then specify
which text is to be used when an exception is raised by passing one of these
constants to the import parameter TEXTID of the instance constructor.
All exception classes inherit the KERNEL_ERRID attribute from CX_ROOT.
This attribute contains the name of the appropriate runtime error if the exception
was raised by the runtime environment - such as BCD_ZERODIVIDE if the
program catches a CX_SY_ZERODIVIDE exception (division by zero). If the
exception is not listed, a runtime error occurs.
An exception can only be handled if the statement that could raise it is enclosed
in a TRY-ENDTRY block. The exception is then handled using the CATCH
statement in the TRY-ENDTRY block.
CLASS lcl_plane
DEFINITION.
PUBLIC SECTION.
...
METHODS get_attributes
EXPORTING
ex_name TYPE t_name_15
value(ex_wa_plane) TYPE saplane
RAISING
cx_bc401_excd_planetype.
ENDCLASS. "lcl_plane DEFINITION
CLASS lcl_plane IMPLEMENTATION.
...
METHOD get_attributes.
SELECT SINGLE * FROM saplane
INTO ex_wa_plane
WHERE planetype = me->type.
IF sy-subrc = 0.
IF ex_wa_plane-seatsmax_b = 0. "should stand for freighter here
RAISE EXCEPTION TYPE cx_bc401_excd_planetype
EXPORTING
planetype = me->type
textid = cx_bc401_excd_planetype=>cx_bc401_excd_planetype_f.
ENDIF.
ELSE.
RAISE EXCEPTION TYPE cx_bc401_excd_planetype
EXPORTING
planetype = me->type.
ENDIF.
ENDMETHOD. "lcl_plane
ENDCLASS. "lcl_plane IMPLEMENTATION
BC401
CLASS lcl_carrier DEFINITION.
PUBLIC SECTION.
...
METHODS display_attributes.
ENDCLASS. "lcl_carrier DEFINITION
CLASS lcl_carrier IMPLEMENTATION.
...
METHOD display_attributes.
DATA:
l_ref_plane TYPE REF TO lcl_plane,
l_name TYPE t_name_15,
l_wa_plane TYPE saplane,
l_ref_exc TYPE REF TO cx_bc401_excd_planetype,
l_exc_text TYPE string.
...
TRY.
CALL METHOD l_ref_plane->get_attributes
IMPORTING
ex_wa_plane = l_wa_plane.
WRITE: l_wa_plane-planetype,
l_wa_plane-producer,
l_wa_plane-seatsmax_b.
CATCH cx_bc401_excd_planetype INTO l_ref_exc.
l_exc_text = l_ref_exc->get_text( ).
WRITE l_exc_text COLOR COL_NEGATIVE.
ENDTRY.
ENDMETHOD. "display_attributes
ENDCLASS. "lcl_carrier IMPLEMENTATION
Reward points if useful,
Aleem.
11-02-2007 8:21 AM
Hi ,
<b>In an exception situation,</b> an exception is represented by an exception object -that is, an instance of an exception class. The attributes of each exception objectcontain information about the error situation.
When handling an exception, you can have it raise new exceptions and thus create
a chain of exceptions. Thus, in special cases, you can catch a runtime exception
and then have it raise an exception of its own.
"You can define exception classes yourself
, but there is already a range of predefined exception classes in the system particularly for exceptions in the runtime environment. You usually create exception classes globally in the Class Builder, but you can also define local exception classes within a program or
global class.
The use of class-based exceptions is not limited to object-oriented contexts.
Class-based exceptions can be raised and handled in all processing blocks.
In particular, all previously catchable runtime errors can now be handled as
class-based exceptions.
If a class-based exception is raised, the system interrupts the normal program flow
and tries to navigate to a suitable handler. If it cannot find a handler, a runtime
error occurs.
All exception classes are derived from the one of the classes CX_NO_CHECK,
CX_DYNAMIC_CHECK, or CX_STATIC_CHECK, themselves derived from
the common superclass CX_ROOT.
Example: Raising, Propagating and Handling an Exception
CLASS lcl_plane
DEFINITION.
PUBLIC SECTION.
...
METHODS get_attributes
EXPORTING
ex_name TYPE t_name_15
value(ex_wa_plane) TYPE saplane
RAISING
cx_bc401_excd_planetype.
ENDCLASS. "lcl_plane DEFINITION
CLASS lcl_plane IMPLEMENTATION.
...
METHOD get_attributes.
SELECT SINGLE * FROM saplane
INTO ex_wa_plane
WHERE planetype = me->type.
IF sy-subrc = 0.
IF ex_wa_plane-seatsmax_b = 0. "should stand for freighter here
RAISE EXCEPTION TYPE cx_bc401_excd_planetype
EXPORTING
planetype = me->type
textid = cx_bc401_excd_planetype=>cx_bc401_excd_planetype_f.
ENDIF.
ELSE.
RAISE EXCEPTION TYPE cx_bc401_excd_planetype
EXPORTING
planetype = me->type.
ENDIF.
ENDMETHOD. "lcl_plane
ENDCLASS. "lcl_plane IMPLEMENTATION
CLASS lcl_carrier DEFINITION.
PUBLIC SECTION.
...
METHODS display_attributes.
ENDCLASS. "lcl_carrier DEFINITION
CLASS lcl_carrier IMPLEMENTATION.
...
METHOD display_attributes.
DATA:
l_ref_plane TYPE REF TO lcl_plane,
l_name TYPE t_name_15,
l_wa_plane TYPE saplane,
l_ref_exc TYPE REF TO cx_bc401_excd_planetype,
l_exc_text TYPE string.
...
TRY.
CALL METHOD l_ref_plane->get_attributes
IMPORTING
ex_wa_plane = l_wa_plane.
WRITE: l_wa_plane-planetype,
l_wa_plane-producer,
l_wa_plane-seatsmax_b.
CATCH cx_bc401_excd_planetype INTO l_ref_exc.
l_exc_text = l_ref_exc->get_text( ).
WRITE l_exc_text COLOR COL_NEGATIVE.
ENDTRY.
ENDMETHOD. "display_attributes
ENDCLASS. "lcl_carrier IMPLEMENTATION
Reward points if it is usefull.....
Girish