You are on page 1of 4

EXCEPTIONS IN OOPS ABAP

STEPS FOR RAISING AND CATCHING EXCEPTIONS


STEP 1 : Create an exception class in SE24 .

Go to SE24 .
Give a name ZCX_EXCEPTION_CLASS .
Create & save it .
Go to TEXT TAB and define exception id and text .

EXCXEPTION
No_kunnr

please enter a customer number .

Invalid_kunnr

This is invalid customer .

Invalid_kunnr2

& kunnr& is an invalid customer .

NOTE : For every EXCEPTION ID , an ATTRIBUTE will be created .


STEP 2 : Create a normal class with a method .
Create a method with name GET_CUST_DET with
IMPORTING IM_KUNNR
and
EXPORTING EX_kna1 .
Click on EXCEPTIONS BUTTON and Select EXCEPTION CLASS CHECKBOX .
Now define an EXCEPTION CLASS which Created previously .
Write the ABAP code and raise the Exception where ever necessary .
IF IM_KUNNR IS INITIAL .
RAISE EXCEPTION TYPE ZCX_EXCEPTION_CLASS .
EXPORTING
TEXTID = ZCX_EXCEPTION_CLASS=> NO_KUNNR .
ELSE .
SELECT
abap code
. .
IF SY-SUBRC <>0
RAISE EXCEPTION TYPE ZCX_EXCEPTION_CLASS .
EXPORTING
TEXTID = ZCX_EXCEPTION_CLASS=> INVALID_KUNNR >
KUNNR = IM_KUNNR .
ENDIF .
ENDIF .

STEP3 : CATCHING THE EXCETIONS .

Exception should be handled using TRY ..CATCHEND TRY .


If an Exception is Raised it is caught in the exception calss .
So , for this class we should create an object .
Once an object is created , call the method GET_TEXT which gives the TEXT
of an EXCEPTION ID
CREATE AN ABAP PROGRAM AND CALL THE ABOVE METHOD .

ABAP PROGRAM :
DATA : OBJ TYPE REF TO ZCL-SAMPLE .
DATA : OBJ_EXC TYPE REF TO ZCX_EXCEPTION_CLASS .
DATA : V_MESSAGE TYPE STRING .
TRY .
CALL METHOD OBJ GET_CUST_DET .
EXPORTING
IM_KUNNR = P_KUNNR .
IMPORTING
EX_KNA1

= WA_KNA1 .

CATCH ZCXEXCEPTION_CLASS INTO OBJ_EXC.


CALL METHOD OBJ_EXC IF_MESSAGE ~GET_TEXT .
RECEIVING
RESULT = V_MESSAGE .
MESSAGE
ENDTRY .

V_MESSAGE TYPE

E .

EXCEPTION CLASS USING MESSAGE CLASS .


STEP 1 :
SE91 is TCODE for creating Message class .
Define all the messages in this class .
Message no

Message short text

000
001
002

INVALID_KUNNR
NO_KUNNR
& IS INAVLID KUNNR

..

999 .
Click mon save .
STEP 2 :
Create an exception class .
Give a name ZCX_EXCEPTION_CLASS .

Select the Message Class CheckBox .


Click on save .
Click on TEXT TAB .
Define the three exception i.e NO_KUNNR .
INVALID_KUNNR
INVALID_KUNNR2 with out any text
because we want to get the text from Message Class(SE91) .
Declare an Attribute KUNNR .
Put the cursor on the first exception NO_KUNNR and click on MESSAGE_TEXT
button .
Provide Message Class name and Message Class number .
Repeat the same for the second exception (INVALID_KUNNR) and
third exception (INVALID_KUNNR2) .
Click on the exception INVALID_KUNNR2 and click on Message TEXT button
and provide message class number .
Select attribute from the drop down i.e. KUNNR .
Save & Activate .
Now Raise the Message in the class methods and catch them in ABAP
program .

You might also like