简体   繁体   中英

How to use exceptions in a function module?

I am pretty new at ABAP and I try to learn Function Modules in ABAP. I create a function and give it parameters for IMPORT and EXPORT,TABLES also I want to give an exception to the user while he/she make something what I don't want.

So, I have two import parameters which are: These lines are the column items for Import and Export fields.

i_x TYPE xx
i_type TYPE char2

I have 1 table parameters which is :

et_xx_sorted LIKE xx 'this is an exception'

I have 1 exception line :

MAX_RECORD 'There is no record for this.'

My source code is :

 SELECT * INTO TABLE et_xx_sorted[] FROM xx WHERE yy = i_x.

I want to use the my exception line when user give an input to i_x which is maximum than the border which I choose. I mean there is numbers 1 to 30 , but I want that user just can give 1 to 20. He/she can not give 20 to 30. And if there is an input into 20 to 30, program needs to give MAX_RECORD exception and say to the user 'There is no record for this.'

I used :

IF sy-subrc <> 0.
    MESSAGE 'No record' TYPE 'E' RAISING MAX_RECORD.
  ENDIF.

But this is not what I want. There is a line for 20 - 30 so this code block isnt work for my border items. There is 1 to 30 lines but user only can see 1 to 20. And if he/she give 20 to 30 then program should give an exception using which I determined into EXCEPTIONS field.

What I understood from your saying I think you can manage it doing something like this;

IF i_x LT 20.  "less than

   SELECT * INTO TABLE et_xx_sorted[] FROM xx WHERE yy = i_x.

ELSEIF i_x GT 20.  "greater than

   MESSAGE 'No record' TYPE 'E' RAISING MAX_RECORD.

ENDIF.

Hope its helpful.

Talha

Basically there are two types of throwing/catching the exception in function modules:

  1. RAISE <exception> .

  2. MESSAGE..... RAISING <exception> .

The first is used when you want to implement manual handling of exception in a caller program. The second one passes the handling to system, basically turning it into message.

How it looks in practice?

The first case: having declared the exception in FM, we throw it like this:

function my_func  
*"-----------------------------------------------  
*"*"Local interface:  
*"         IMPORTING  
*"               VALUE(i_x) TYPE xx
*"               VALUE(i_type) TYPE char2
*"         TABLES
*"               VALUE(et_xx_sorted) LIKE xx
*"         EXCEPTIONS  
*"               MAX_RECORD 
*"------------------------------------------------  

IF i_x GT 20.  "greater than

   RAISE MAX_RECORD.

ENDIF.

In the caller program we handle it like this

DATA(i_x) = 23.
call function 'MY_FUNC'   
 exporting   
  i_x = i_x
  i_type = 'type'  
 tables
  et_xx_sorted = itab
 exceptions     
  max_record = 1   
  others = 2.  
  
if sy-sybrc eq 0.   
 write: / ` Input ` && i_x && ` is correct`  
else.   
 write 'There is no record for this'.  
endif. 

The second case: we may not even handle it in the caller program, the system do it by itself.

Answering your question about short text declared in FM interface: it is not used in exception handling, it is used only for documenting purposes.

Look the documentation about exceptions in function modules: https://help.sap.com/saphelp_nw73/helpdata/en/d1/801f1c454211d189710000e8322d00/content.htm?no_cache=true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM