Raise Exception's in Function Module
Raise Exception's in Function Module
https://musicodez.wordpress.com/2010/09/12/raising-class-exception-caused-by-fmexception/
http://sapabaptutorial.blogspot.in/2009/09/using-abap-exception-classes-with.html
method SEND_EMAIL.
DATA:
lwa_doc
TYPE sodocchgi1,
li_receivers type ptreq_email_receivers_tab,
lwa_receiver type somlreci1.
CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
EXPORTING
document_data
= lwa_doc
tables
receivers
= li_receivers
EXCEPTIONS
TOO_MANY_RECEIVERS
= 1
DOCUMENT_NOT_SENT
= 2
DOCUMENT_TYPE_NOT_EXIST
= 3
OPERATION_NO_AUTHORIZATION
= 4
PARAMETER_ERROR
= 5
X_ERROR
= 6
ENQUEUE_ERROR
= 7
OTHERS
= 8.
if sy-subrc <> 0.
*
message id sy-msgid type sy-msgty number sy-msgno
*
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
endmethod.
This FM will raise error DOCUMENT_NOT_SENT if we execute.
2. Create custom exception class
Now, we need to handle the exception of the FM. We need to create a custom exception class, for
example ZCX_TESTCUSTOMEXCEPTION.
a. Go to SE24, create new class ZCX_TESTCUSTOMEXCEPTION.
b. Pick class type Exception Class and Superclass CX_BO_APPLICATION. Save.
c. Go to Text tab, and put &MESSAGE& as text of the exception ID <your exception name>.
Activate.
d. Go to Attributes tab, create new attribute MESSAGE with level Instance, visibility Public, type
String. (note: the name of the attribute must be the same with variable name put in the text field of
your exception in the Text tab). Activate.
b. Inside sy-subrc <> 0 block after the FM call, we will need to raise our custom exception and send
the message to our custom exception.
c. Use Pattern button to create object pattern, choose ABAP object pattern, and raise exception
ZCX_TESTCUSTOMEXCEPTION. In the exception call, well see our custom parameter message, to
which we will send the message.
if sy-subrc <> 0.
DATA: lv_message TYPE string.
lv_message = 'Error while sending email'.
RAISE EXCEPTION TYPE zcx_testcustomexception
EXPORTING
message = lv_message.
endif.
4. Catch the exception from calling program
Now in the calling program:
form test_exception_handling .
data:
lo_object
type ref to zcl_testclass,
lx_testexception type ref to zcx_testcustomexception,
lv_error_text
type string.
create object lo_object
exporting
i_purchase_order = '4500000218'.
try.
call method lo_object->send_email.
catch zcx_testcustomexception into lx_testexception.
lv_error_text = lx_testexception->get_text( ).
endtry.
write lv_error_text.
endform.
" test_exception_handling
Please notice that in this example, we put hardcoded message Error while sending email for all non
zero sy-subrc value. We could, for example, put different message for every sy-subrc value. Its also
possible to use message class parameters if the FM exception raise message class parameters.
Example:
if sy-subrc ne 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 into lv_message.
raise exception type zcx_mpu_po_ctrval
exporting
message = lv_message.
endif.
However, the FM SO_NEW_DOCUMENT_SEND_API1 doesnt return message class parameters during
its exception raise, so we should hardcode the message.
A more elegant way to raise custom exception is using message class, which you could refer to this
tutorial:
http://sapabaptutorial.blogspot.com/2009/09/using-abap-exception-classes-with.html
Lets analyze this statement. First, we state the type of the exception, which is CX_DB_ERROR
(thats the exception that we created in the previous post). Next, were passing the parameters to
the exception.
The first parameter answers the question What is the message we want to display? It states
the key of the text that we want to insert. Since we want to display a text for a database write
error, we pass the key WRITE_ERROR (Remember it from last time? We created it on step 6).
The second parameter answers the question What are the parameters needed for the message
we want to display? If you remember, our message had one parameter, the table name, so that
we could imply that the write error occurred on this or that table. So here we pass the name of
the table, in this example its the famous SPFLI.
So far weve looked on how to throw the exception. Now lets take a look at the code for catching
the exception.
In this simple code example, we catch the exception CX_DB_ROOT into a variable called OREF. We
then extract the exception text into the variable TEXT by using the GET_TEXT() method.
At the end of this code execution, the variable TEXT should hold the following string:
An error occurred when writing to table SPFLI.
Note: There is another parameter which is commonly passed when creating an exception. It is
called PREVIOUS, and its used to wrap exception in case of an exception chain. Those of you who
have experience with Java or .NET are probably familiar with this concept. To all the others, dont
worry well mention exception chains later on.