while defining the exceptions you have to add them under 'Exception" tab in SE37.
In code you need to raise the exception on specific condition.
You will handle this exception once its returned in FM call
CALL FUNCTION 'Z_FM_NAME' EXPORTING f_objid = p0001-orgeh = '01' IMPORTING f_hco_id = int_hist-comp EXCEPTIONS error_obj_org_not_found = 1 error_invalid_org = 2 error_parent_org_not_found = 3 OTHERS = 4.
You can handle the exceptions by using a structure BAPIE1RET2 which returns the messages captured in BAPI. In Tables parameter of your BAPI, you can create a structure as of type BAPIE1RET2 so that you can capture the return parameters from BAPI.
Thanks and Regards,
to handle the Exceptions,
1. You have to declare the exceptions in Exceptions section in SE37.
2. Use Raise statement to raise the exception When the particular condition reached.
Eg: when you are dividing A with B,
if B = 0.
Raise B_Zero [With message I001(ZZZ)]
3. When you call this function module you have to assign some value in exceptions section.
Eg: Call function 'ZDIVIDE'
a = a
b = b
result = r
B_Zero = 1
others = 99.
4. If you dont specify message extension in Raise statement, then you should use the code like this
if Sy-subrc = 1.
message E001(00) with 'Should not divide with zero'.
Why can't we use exceptions in BAPI and RFC ?
The reason is that raising exceptions is a feature handled by SAP internally. When dealing with RFCs and BAPIs, you have to keep in mind that these are used by external applications also, which cannot handle exceptions raised in SAP.
That is the reason you need to return the messages in RETURN parameter, which can be a table (for multiple messages) or a structure.