First we can discuss what is exceptions exactly.
Exceptions: The Exception is a problem that arises during execution of the program. When an exception occurs the normal flow of an execution is disrupted and the program application is terminating abnormally, which is not recommended, by avoiding these you can handle the errors. When you fail to capture the error the runtime system will abort the program (i.e., crash)
Whenever the exceptions are raised in case of ABAP the program is going to short dump, this is not the right case, as a developer you can think about the exceptions where it is raised from where you need to capture the exceptions, Exceptions are always play an important role while developing the application.
What is exception handling ?
Exception handling is a process of handling an exception in such a way the short dump of a program is avoided and normal termination of the program is achieved. If the generated exception successfully handled the normal termination of the program is achieved and the performance of the software is not affected.
How to handle the exceptions ?
Exceptions provide a way to transfer control from one part of a program to another. ABAP exception handling is built up on 3 keywords, RAISE, TRY, CATCH. Assuming a block will raise an exception, a method catches an exception using a combination of Try & catch keywords.
A try block is placed around the code that might generate an exception.
Try: The try block contains the application coding whose exceptions are to be handled, the statement block is processed sequentially. It can contain further control structures and calls of procedure or other ABAP programs.
Catch: Catch block is used to capture the exceptions which might be generated in the try block.
Exceptions Hierarchy
CX_ROOT : It is the parent class of all the exceptions which is generated by sap.
CX_DYNAMIC_CHECK : It is checked only at runtime system. All the system raised exceptions comes under this exception.
CX_STATIC_CHECK : It is checked by both the compiler and runtime system. If any exception occurred in this category, it’s not handle internally, we need to declare RAISING clause of the procedure interface.
CX_NO_CHECK : we are declared these CX_NO_CHECK exceptions always implicitly.
Here i will show you,
1. How to create an exception class and what are the steps to be followed.
2. Different ways of creating exception class.
In se24 we are creating the exception class the super class of that exception class is CX_STATIC_CHECK.
They are 2 ways we can create the exception class in se24.
1. Exception class with messages .
2. Exception class with out messages.
First let me discuss Exception class with out message class
Steps to create exception class without selecting message class.
1. Goto SE24
2. Provide Object Type Name starting ‘ZCX’
Figure : 1
3. Click on create button and it shows below dialog box
Figure : 2
4. In Figure 2., dialog I am un-checking the with messages of message class and i highlighted above it is automatically showing the super class is CX_STATIC_CHECK.
Click on save button.
Figure : 3
5. It Is asking the package name, provide package name and click on save button.
Figure : 4
6. In fig.4 provide work bench request and click on save.
Figure : 5
7. In Fig. 5, you can see it is automatically populated the super class methods.
Figure : 6
8. In above fig : 6, observe the super class name CX_STATIC_CHECK, i.e. the methods which are present in Super class automatically populated in the existing class.
9. click on texts tab. Enter few texts which you need to raise the error messages.
Figure : 7
10. Activate the class.
Below I created a report program (ZK_EXCEPTIONS) here I am taking 2 parameters, 1 for checking the name and another is for checking the age.
CASE 1: if the name is not equal to kiran I raise the error message.
CASE 2: if the age is below 18, we can raise the error message.
&---------------------------------------------------------------------*
*& Report ZK_EXCEPTIONS
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZK_EXCEPTIONS.
DATA : GO_OBJ TYPE REF TO ZCX_K_EXCEPTIONS. " OBJECT REFERENCE OF THE EXCEPTON CLASS
CONSTANTS : C_NAME TYPE STRING VALUE 'KIRAN'.
PARAMETERS : P_VALUE TYPE C LENGTH 10,
P_DATE TYPE SY-DATUM.
DATA : LV_STR TYPE STRING.
* LV_CURR_DATE TYPE I.
AT SELECTION-SCREEN.
TRY.
IF P_VALUE <> C_NAME.
RAISE EXCEPTION TYPE ZCX_K_EXCEPTIONS
EXPORTING
TEXTID = ZCX_K_EXCEPTIONS=>ZCX_K_EXCEPTIONS
.
ENDIF.
IF P_DATE IS NOT INITIAL.
DATA(LV_DATE) = P_DATE+0(4). " CHECKING DATE OF BRTH
DATA(LV_CURR_dATE) = SY-DATUM+0(4). " CHECKING CURRENT DATE
DATA(LV_AGE) = LV_CURR_DATE - LV_DATE. "FINDING THE AGE OF A PERSON
IF LV_AGE < 18.
RAISE EXCEPTION TYPE ZCX_K_EXCEPTIONS
EXPORTING
TEXTID = ZCX_K_EXCEPTIONS=>LESS_AGE
.
ENDIF.
ENDIF.
CATCH ZCX_K_EXCEPTIONS INTO GO_OBJ.
* LV_sTR = GO_OBJ->GET_TEXT( ).
* WRITE : LV_sTR.
MESSAGE GO_OBJ TYPE 'E'.
ENDTRY.
Steps to create the exception class with message class
1. Go to se24 and give the class name and select with messages as highlighted below and save in your own package.
Figure : 8
2. Now check the text tab in below, compare with no exception class and with exception class. Below highlighted button added with message class.
Figure : 9
3. Go to attribute tab and declare one variable lv_carrid as shown below.
Figure : 10
4. Now go to texts tab create the text for exception with message class. Give the User defined name here I given (INVALID_CARRID) and click on Message Text it will ask message class and number and in message text will auto populate once you given the message number and add attribute if you want. Activate the class.
Figure : 11
5. Go to se38 create the program. Below are the coding part
*&---------------------------------------------------------------------*
*& Report ZKI_EXCEPTION_CLASS
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZKI_EXCEPTION_CLASS.
PARAMETERS : P_CARRID TYPE SPFLI-CARRID.
DATA(lo_obj) = NEW ZCX_K_EXP_WITH_MSG_CLASS( ).
AT SELECTION-SCREEN.
TRY.
SELECT SINGLE CARRID FROM SPFLI INTO @DATA(LV_CARRID) WHERE CARRID = @P_CARRID.
IF SY-SUBRC <> 0.
RAISE EXCEPTION TYPE ZCX_K_EXP_WITH_MSG_CLASS
EXPORTING
TEXTID = ZCX_K_EXP_WITH_MSG_CLASS=>INVALID_CARRID
* PREVIOUS =
LV_CARRID = P_CARRID
.
ENDIF.
CATCH ZCX_K_EXP_WITH_MSG_CLASS INTO lo_obj.
MESSAGE lo_obj TYPE 'E'.
ENDTRY.
If you enter invalid carrid it is throwing the error like below with user text as highlighted below.
Figure : 12
Finally this blog post covers how to raise the user define exceptions in report level using exceptions class in se-24, and also you can learn how to create the exceptions class with out messages and with message class, and also this blog post covers over view of exceptions.
Source: sap.com
No comments:
Post a Comment