Você está na página 1de 8

References:

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

Raising Class Exception Caused by FM Exception


Raising class exception that caused by function module exception
I once cross a condition where I want to forward an exception from function module that is called in a
public method, so that when the program called the method and the FM raise an error, it could catch
exception exactly as the function module raise, and put it in the log. I dont really sure if the method
below is the best practice, but it can handle the situation properly.
1. FM calls inside a method
For example here, I want the program to be able to catch exception of method SEND_EMAIL, which
inside contains call of FM SO_NEW_DOCUMENT_SEND_API1, a FM to send email.
Inside the method, we would put a simple code below:

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.

3. Raise the custom exception inside the method


Go back to our method SEND_EMAIL, now we will raise the exception ZCX_TESTCUSTOMEXCEPTION if
the FM raise an error.
a. In class definition, go to Method tab, register ZCX_TESTCUSTOMEXCEPTION to method
SEND_EMAIL.

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.

d. Put below code inside the sy-subrc check after FM calls:

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

and when we perform the method, it will show the error.

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

Using ABAP Exception classes with Message classes (Part 1 of 2)


Weve talked about Exception Classes, and weve talked aboutMessage Classes. Now
lets see how we can combine these two together.
Lets take a scenario, in which you have an exception that is thrown whenever a
database error occurs in your program. Lets call this exception class CX_DB_ERROR.
We want our exception to display various messages that can also be translated later
on to different languages. For example, Error when writing to database or Error
when reading from database. So lets combine ABAP Exception and Message
classes.
1. First, create a message class through SE91. For this examples sake, lets call it
CM_DATABASE_MESSAGES
2. Add a few messages to the message class. For our example, lets add the
message: An error occurred when writing to table &1. &1 indicates that a
parameter would be inserted to the message. This parameter would be the name of
the table that caused the error. Well get back to this in a few minutes.

3. Now open SE80 and create the exception class CX_DB_ERROR .


4. Remember that our message from step 2 states which table caused the error?
Nows the time to add this parameter. So click on the Attributes tab, and add an
attribute for the table name. Lets call it TABLE_NAME, and give it the type STRING.
5. Next, click on the Texts tab.
6. Enter a key to the message you would like to display on your exception class. In
our example, lets add the key WRITE_ERROR, indicating an error occurred when
writing to the database.
7. Click on the Message Text button.
8. A new dialog opens, titled Assign Attributes of an Exception Class to a Message.
9. In the field Message Class, insert the name of the Message Class associated
with your exception class. Lets insert the class CM_DATABASE_MESSAGES, which we
created earlier.
10. Now we need to choose the message number. Click on the choice button and
choose the message An error occurred when writing to table &1. The number of
the message should now appear in the field (as we created only one message in our
example, it should be 000).
11. Now, you have to choose attributes that would be associated with the
parameter &1. Click on the drop down list button, and choose the TABLE_NAME
attribute we created earlier.
12. Save everything and activate.
Youve just finished creating an exception class that uses a message class. In
the next post, Ill show you how to use the exception class with the message.

Using ABAP Exception classes with Message classes (Part 2 of 2)


In the previous post, we created a simple ABAP exception class that uses a Message Class. Now
lets see how we can use this class. Its strongly recommended that you read part one before
continuing.
The statement for raising (or throwing) an exception class that uses a Message class looks as
follows:
raise exception type CX_DB_ERROR
exporting TEXTID = CX_DB_ERROR=>WRITE_ERROR
TABLENAME = SPFLI.

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.

data OREF type ref to CX_ROOT.data TEXT type STRING.


try.
... Some method that throws CX_DB_ERROR
catch CX_DB_ERROR into OREF.
TEXT = OREF->GET_TEXT( ).
endtry.

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.

Você também pode gostar