1

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.

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
Merve Gül
  • 1,377
  • 6
  • 23
  • 40

2 Answers2

2

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

Mtu
  • 643
  • 10
  • 18
  • I am using this one now, what I want is; we are defining the exception into function module 'MAX_RECORD' and writing its short text 'There is no record'. But when we use the exception we are writing MESSAGE 'No record' TYPE 'E' RAISING MAX_RECORD. BUT, I want to call MAX_RECORD's short text into the exception and show it under the page. If we dont use short text why we are defining this? I wish I can tell you my problem into Turkish, I hope this answer would be understandable for you – Merve Gül Jun 21 '12 at 10:21
  • Merve, MAX_RECORD is not a standard function module, If you created a such Fucntion modul, you should rename it something like Z_MAX_RECORD or Y_MAX_RECORD. If its not a standard, we need to use Z or Y on custom created objects. My above code should do what you have implied but if you really want to use a function module, here is a quick tutorial for it: [link]http://www.saptechnical.com/Tutorials/ABAP/FunctionModule/page2.htm With this tutorial you will be able to create your own function module and you can use that by clicking the "PATTERN" button in ABAP editor(se38). – Mtu Jun 21 '12 at 14:18
0

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

Suncatcher
  • 10,355
  • 10
  • 52
  • 90