06-02-2008 12:28 PM
Hi,
Can anyone give the simple example of Exception Handling using function module...
Thanks in Advance.
06-02-2008 12:47 PM
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 this example, a TRY-ENDTRY structure is nested in the TRY block of a different TRY-ENDTRY structure. The following four scenarios are demonstrated in the example:
Catching an Exception by Handling a Superclass
If NUMBER is greater than 100, the exception CX_DEMO_ABS_TOO_LARGE that is self-defined in the Exception Builder of the ABAP Workbench is raised in the TRY block of the external TRY-ENDTRY structure. This exception is the subordinate class of the most general exception, CX_ROOT, and is handled by the second CATCH block of the same TRY-ENDTRY structure.
Catching an Exception by Handling the Suitable Class
If NUMBER is equal to zero, the exception CX_SY_ZERODIVIDE predefined in the system is raised as a result of the division in the TRY block of the internal TRY-ENDTRY structure and handled in the corresponding CATCH block of the same TRY-ENDTRY structure.
Executing a CLEANUP Block Before Catching an Exception
If NUMBER is a negative number, the exception CX_SY_ARG_OUT_OF_DOMAIN predefined in the system is raised in the TRY block of the internal TRY-ENDTRY structure using the SQRT function. Since a handler is not defined for this exception in the internal TRY-ENDTRY structure but is defined in the external TRY-ENDTRY structure, the CLEANUP block of the internal TRY-ENDTRY structure is executed. The exception is then handled in the first CATCH block of the external TRY-ENDTRY structure, since CX_SY_ARG_OUT_OF_DOMAIN is the subordinate class of CX_SY_ARITHMETIC_ERROR.
No Exception
In all other cases, an exception is not raised and the TRY blocks of both TRY-ENDTRYstructures are fully processed.
06-02-2008 12:49 PM
Hi,
While creating a fn. module you can give exceptions which can occur while calling the function module.
Later on wherever you call fn module you can handle exceptions by checking value of SY-SUBRC.
case sy-subrc.
case1 : write code handling exception1 .
case2:
case n.
in this way you can handle all exceptions given by you in fn module.
Reward points if helpful.
06-02-2008 12:50 PM
Triggering Exceptions
Within a function module, you can address all exceptions using the names you defined in the interface. Exceptions can be handled either by the system or by the calling program. You decide this when you call the function, by assigning a numeric value to the exceptions that you want to handle yourself. For further information, see Calling Function Modules From Your Programs.
Exceptions must be explicitly triggered.
There are two ABAP statements that may only be used in function modules that you can use to trigger exceptions:
Syntax
RAISE <Exception>.
MESSAGE..... RAISING <Exception>.
The effect of these statements depends on whether you handle the exception in the calling program or let the system process it.
If you trigger the exception in the RAISE statement and the calling program is to handle it, the function module processing is terminated, and the numeric value assigned to the exception is placed in the system field SY-SUBRC. Further processing then takes place in the calling program.
If the calling program fails to handle the exception, the system triggers a runtime error.
If you use the MESSAGE... RAISING statement, the processing is similar if you want to handle the exception in the calling program. If you want the system to handle the exception, there is no runtime error generated in this case. Instead, processing continues, and the system displays a message with the defined type. To do this, you must specify the MESSAGE-ID in the first statement of the include program L<fgrp>TOP. The MESSAGE... RAISING statement also enters values in the following system fields:
o SY-MSGID (message ID)
o SY-MSGTY (message type)
o SY-MSGNO (message number)
o SY-MSGV1 to SY-MSGV4 (contents of the fields <f1> to <f4> that are included in the message).
For further information, see the keyword documentation for the MESSAGE statement.
06-02-2008 12:51 PM
check this also code to manage exceptiopn
http://help.sap.com/saphelp_nw04/helpdata/en/d1/801f1c454211d189710000e8322d00/content.htm
06-02-2008 12:54 PM
WHEN CREATING FUNCTION MODULES you can define exception
the calling program determines whether and which exception is to handle itself
you can assign the same error number to several exceptions
sample code is:
CALL FUNCTION "Zfill_seattab'
Exporting
YEAR = YEAR
TABLES
seattab = itab
EXCEPTIONS
NO_ENTRY = 1
OTHER_ERROR = 2
CASE SY-SUBRC.
WHEN 1.
WRITE 'NO ENTRY'.
WHEN2 .
WRITE 'OTHER'ERROR' .
thanks
reward points if useful
Edited by: Richa Khosla on Jun 2, 2008 1:54 PM
Edited by: Richa Khosla on Jun 2, 2008 2:00 PM
06-02-2008 6:11 PM
Hi,
while creating the function module...there you can define exceptions in exceptions tab.
Aftre that you need to raise the exception...where you want that to be raised.
Ex: (Function Module code)
If v_num is initial.
RAISE EXCEPTIONNAME.
endif.
And after this you need to handle this, in your main program like this.
Case sy-subrc.
case 1.
some code to handle exception 1.(you can dispaly messages).
case 2.
some code to handle exception 2.(you can dispaly messages).
endcase.
Regards
Sandeep Reddy