Modularization & Catch Statement

4,839 views
4,693 views

Published on

5th part of series shared on http://sapdocs.info/sap/abap/documents-for-sap-abap-beginners/

Published in: Business, Sports
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,839
On SlideShare
0
From Embeds
0
Number of Embeds
700
Actions
Shares
0
Downloads
751
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Modularization & Catch Statement

  1. 1. ABAP Chapter 5 <ul><li>Modularization </li></ul><ul><li>Catch Statement </li></ul>
  2. 2. Modularization
  3. 3. Modularization <ul><li>Internal Subroutine Call </li></ul><ul><li>External Subroutine Call </li></ul><ul><li>Function Module </li></ul>
  4. 4. Subroutine START-OF-SELECTION. Perfrom routine1. Perform routine2. Perform routine2. Form routine1. select * from customers into table tab. Endform. Form routine2. loop at tab. write: / tab-id,tab-name. endloop. Endform. …
  5. 5. Modularization <ul><li>Avoid redundancy </li></ul><ul><li>Make your program easy to read & improve their structure </li></ul><ul><li>Re-use Program components </li></ul>
  6. 6. Calling and Defining Subroutines REPORT ztest. * Global Data TABLES customers. DATA tmp type i. * Subroutine Calls PERFORM routine1. PERFORM routine2. * Subroutine FORM routine1. DATA tmp1 type p. “Local data write tmp. ENDFORM . FORM routine2. DATA tmp2(10). “Local data … .. ENDFORM .
  7. 7. Call by Value a1 Memory Space(Subroutine) f1 Copy
  8. 8. Call by Value Data: a1,a2. a1 = ‘A’. a2 = ‘A’. PERFORM routine1 USING a1 a2. .…... FORM routine1 USING VALUE(f1) VALUE(f2). f1 = ‘X’. f2 = ‘X’. ENDFORM.
  9. 9. Call by Reference a3 Memory Space(Subroutine) f3 Address Passing
  10. 10. Call by Reference Data: a3. a3 = ‘A’. PERFORM routine2 USING a3. .…... FORM routine2 USING f3. f3 = ‘X’. ENDFORM.
  11. 11. Call by Value and Result a4 Memory Space(Subroutine) f4 Copy Copy
  12. 12. Call by Value and Result Data: a4,a5. a4 = ‘A’. a5 = ‘A’. PERFORM routine3 USING a4 a5. .…... FORM routine 3 CHANGING VALUE(f4) f5. “ f5 : call by reference f4 = ‘X’. f5 = ‘X’. ENDFORM.
  13. 13. Passing Structure as Parameters TABLES sflight. SELECT * FROM sflight. PERFORM subproc USING sflight. ENDSELECT. FORM subproc USING rec LIKE sflight. WRITE: / rec-carrid. ENDFORM.
  14. 14. Passing Internal Table as Parameters DATA: tab LIKE sflight OCCURS 0 WITH HEADER LINE. … PERFORM sub TABLES tab.
  15. 15. Passing Internal Table as Parameters FORM sub TABLES tab1 STRUCTURE tab. LOOP AT tab1. WRITE: / tab1-carrid. ENDLOOP. ENDFORM.
  16. 16. External Subroutines REPORT RSAAA10F. TABLES: sflight. … .. PERFORM cal(RSAAA10B). REPORT RSAAA10B. TABLES sflight. … .. FORM cal. … .. ENDFORM.
  17. 17. EXIT Statement DATA tmp TYPE I. tmp = 4. PERFORM a. WRITE tmp. FORM a. EXIT. tmp = 99. ENDFORM.
  18. 18. STOP Statement DATA tmp TYPE I. START-OF-SELECTION. tmp = 4. PERFORM a. WRITE tmp. END-OF-SELECTION. tmp = 0. write tmp. FORM a. STOP. “go to END-OF-SELECTION tmp = 99. ENDFORM.
  19. 19. Function Module
  20. 20. Function Module <ul><li>Function Group </li></ul><ul><li>Function Library </li></ul><ul><li>- Admin </li></ul><ul><li>- Import/Export Parameter </li></ul><ul><li>- Source Code </li></ul><ul><li>- Main Program </li></ul><ul><li>- Documentation </li></ul>
  21. 21. Function Group <ul><li>When you create a function module, you must assign it to function group </li></ul><ul><li>The function group is the main program in which a function module is embedded </li></ul><ul><li>The function group is a program type F,and not executable </li></ul><ul><li>The entire function group is loaded in a program the first time that you call a function module that belongs to it </li></ul>
  22. 22. Function Group <ul><li>is a container for function modules </li></ul><ul><li>When a function module is called,the entire function group is loaded into the session of the program </li></ul><ul><li>Function group is used to define global data for function modules </li></ul><ul><li>A DATA statement in the global memory of a function group is shared by all the function modules that belong to that function group </li></ul>
  23. 23. Function Group : SE37
  24. 24. Function Group : SE80
  25. 25. Function Module <ul><li>is a code that can be called from any ABAP program,therefore making it a globally accessible object </li></ul><ul><li>ABAP program pass data to function module from import parameters or internal tables </li></ul><ul><li>Function module receives data from a program,process the information in its own code, and then sends back information in the export parameters or internal tables </li></ul>
  26. 26. Function Module : SE37
  27. 27. Function Module
  28. 28. Function Module : Source Code FUNCTION Z_FMTEST. result = number1 ** number2. ENDFUNCTION.
  29. 29. Program Example I REPORT ztest. PARAMETERS: no1 TYPE I , no2 TYPE I. DATA result TYPE I . START-OF-SELECTION. CALL FUNCTION ‘Z_ FMTEST ’ EXPORTING number1 = no1 number2 = no2 IMPORTING result = result. write: / result.
  30. 30. Exercise : Function Module ? ABAP Program Function Module
  31. 31. EXCEPTIONS
  32. 32. Function Module Function Z_CAL01. if number1 > 9 and number2 > 9. raise invalidnumber. else. result = number1 ** number2. endif. ENDFUNCTION.
  33. 33. Example II : Exceptions REPORT ztest. PARAMETERS: no1 TYPE I , no2 TYPE I. DATA result TYPE I . START-OF-SELECTION. CALL FUNCTION ‘Z_ CAL01 ’ EXPORTING number1 = no1 number2 = no2 IMPORTING result = result EXCEPTIONS invalidnumber = 1. IF sy-subrc <> 0. write: / ‘Please enter number < 10’. ELSE. write: / result. ENDIF.
  34. 34. Exercise : Exceptions ? ABAP Program Function Module
  35. 35. EXCEPTIONS VS AT SELECTION-SCREEN FUNCTION Z_CAL01. if number1 > 9 and number2 > 9. raise invalidnumber. else. result = number1 ** number2. endif. ENDFUNCTION. REPORT ztest. Parameters: no1 type i, no2 type i. At selection-screen if no1 > 9 and no2 > 9. message e000(38) with ‘Please enter no < 10’. endif. START-OF-SELECTION. CALL FUNCTION ‘Z_CAL01’. … .. VS
  36. 36. Optional ABAP Program Function Module
  37. 37. Structure in Function Module
  38. 38. Example : Structure
  39. 39. Example : Structure
  40. 40. Internal Table in Function Module
  41. 41. Example : Internal Table
  42. 42. Example : Internal Table
  43. 43. Function Group Function Group : ZGRP00 Function Module : Z_FMTEST Function Module : Z_CAL01
  44. 44. Function Group
  45. 45. Function Module in Function Group
  46. 46. Exercise <ul><li>Display current month name using function module </li></ul>
  47. 47. Catch Statement
  48. 48. CATCH Statement <ul><li>Syntax </li></ul><ul><li>Catch system-exceptions < error type > = < n > . </li></ul><ul><li>< ABAP statement – generate runtime error > . </li></ul><ul><li>Endcatch. </li></ul><ul><li>if sy-subrc = < n >. </li></ul><ul><li>... </li></ul><ul><li>endif. </li></ul>
  49. 49. CATCH Error Type <ul><li>Error class </li></ul><ul><ul><li>Catch system-exceptions conversion_errors = 1. </li></ul></ul><ul><li>Single error </li></ul><ul><ul><li>Catch system-exceptions convt_no_number = 1. </li></ul></ul><ul><li>All catchable runtime error </li></ul><ul><ul><li>Catch system-exceptions others = 1. </li></ul></ul>
  50. 50. CATCH Statement Report ztest. Data num type I. Catch system-exceptions conversion_errors = 1. ”others Move ‘abc’ to num. “runtime error: convt_no_number Endcatch. If sy-subrc = 1. Write: / ‘Assign wrong data type to variable: num’. Endif.
  51. 51. CATCH Statement Report ztest. Data num type I. Catch system-exceptions other s = 1. Move ‘abc’ to num. Endcatch. If sy-subrc = 1. Write: / ‘Assign wrong data type to variable: num’. Endif.
  52. 52. CATCH Statement Report ztest. PARAMETERS: NUM1 TYPE I, NUM2 TYPE I. DATA RESULT TYPE I. START-OF-SELECTION. CATCH SYSTEM-EXCEPTIONS COMPUTE_INT_ZERODIVIDE = 1. RESULT = NUM1 / NUM2. ENDCATCH. IF SY-SUBRC = 1. WRITE: /'Divide by zero'. ELSE. WRITE: / RESULT. ENDIF.
  53. 53. CATCH Statement Report ztest. PARAMETERS: NUM1 TYPE I, NUM2 TYPE I. DATA RESULT TYPE I. START-OF-SELECTION. CATCH SYSTEM-EXCEPTIONS OTHERS = 1. RESULT = NUM1 / NUM2. ENDCATCH. IF SY-SUBRC = 1. WRITE: /'Divide by zero'. ELSE. WRITE: / RESULT. ENDIF.
  54. 54. CATCH in Function Module Function Z_CAL. if number1 > 9 and number2 > 9. raise invalidnumber. else. result = number1 ** number2. endif. ENDFUNCTION. Function Z_CAL. CATCH SYSTEM-EXCEPTIONS OTHERS = 1. RESULT = NUMBER1 ** NUMBER2. ENDCATCH. IF SY-SUBRC = 1. RAISE invalidnumber. ENDIF. ENDFUNCTION.
  55. 55. ABAP Practice

×