OOPS ABAP: Exception class without Message class
Exception Class:
- Exception is a problem that arises during the execution of the program.
- The purpose of exception class is to raise and handle the exceptions.
- We use TRY block to raise the exception and CATCH block to handle the exception.
- The exception class name must start with CX.
- CX_ROOT is the global class(Parent class) for all the exceptions.
There are 3 sub classes for parent class CX_ROOT.
1.CX_STATIC_CHECK - It is checked by both the compiler and runtime systems.
2.CX_DYNAMIC_CHECK - It is checked only at run time.
3.CX_NO_CHECK - It is not checked either by compiler or run time systems.
Note: While creating the exception class,system by default provide CX_STATIC_CHECK as the super class.
Example:
Steps:
- Go to se24 and enter class name.(Eg: ZCX_STU).
- Select radio button ' Exception class' and Uncheck the check box 'with messages of message classes as exception texts'.
- Press enter.
- Click on 'Attributes' tab and enter variable lv_id.
- Enter exception ID and text.
- Click on check and activate.
*&---------------------------------------------------------------------*
*& Report ZVB_STU_EXC_CLS
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zvb_stu_exc_cls.
*TABLES: zvb_stu.
PARAMETERS:p_id TYPE zvb_stu-zstu_id.
DATA:lv_id TYPE zstu_id,
lv_name TYPE zstu_name,
lv_dep TYPE zstu_dep.
DATA:lo_exception TYPE REF TO zcx_stu,
lv_msg TYPE string.
AT SELECTION-SCREEN.
TRY.
IF p_id IS INITIAL.
RAISE EXCEPTION TYPE zcx_stu
EXPORTING
textid = zcx_stu=>zcx_mandt
* previous =
.
ENDIF.
CATCH zcx_stu INTO lo_exception.
CALL METHOD lo_exception->if_message~get_text
RECEIVING
result = lv_msg.
MESSAGE lv_msg TYPE 'E'.
ENDTRY.
TRY.
SELECT SINGLE zstu_id zstu_name zstu_dep
FROM zvb_stu
INTO ( lv_id,lv_name,lv_dep )
WHERE zstu_id = p_id.
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE zcx_stu
EXPORTING
textid = zcx_stu=>zcx_stu
* previous =
lv_id = p_id
.
ENDIF.
CATCH zcx_stu INTO lo_exception.
CALL METHOD lo_exception->if_message~get_text
RECEIVING
result = lv_msg.
MESSAGE lv_msg TYPE 'E'.
ENDTRY.
START-OF-SELECTION.
WRITE: LV_ID,
/ LV_NAME,
/ LV_DEP.
*& Report ZVB_STU_EXC_CLS
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zvb_stu_exc_cls.
*TABLES: zvb_stu.
PARAMETERS:p_id TYPE zvb_stu-zstu_id.
DATA:lv_id TYPE zstu_id,
lv_name TYPE zstu_name,
lv_dep TYPE zstu_dep.
DATA:lo_exception TYPE REF TO zcx_stu,
lv_msg TYPE string.
AT SELECTION-SCREEN.
TRY.
IF p_id IS INITIAL.
RAISE EXCEPTION TYPE zcx_stu
EXPORTING
textid = zcx_stu=>zcx_mandt
* previous =
.
ENDIF.
CATCH zcx_stu INTO lo_exception.
CALL METHOD lo_exception->if_message~get_text
RECEIVING
result = lv_msg.
MESSAGE lv_msg TYPE 'E'.
ENDTRY.
TRY.
SELECT SINGLE zstu_id zstu_name zstu_dep
FROM zvb_stu
INTO ( lv_id,lv_name,lv_dep )
WHERE zstu_id = p_id.
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE zcx_stu
EXPORTING
textid = zcx_stu=>zcx_stu
* previous =
lv_id = p_id
.
ENDIF.
CATCH zcx_stu INTO lo_exception.
CALL METHOD lo_exception->if_message~get_text
RECEIVING
result = lv_msg.
MESSAGE lv_msg TYPE 'E'.
ENDTRY.
START-OF-SELECTION.
WRITE: LV_ID,
/ LV_NAME,
/ LV_DEP.
Comments
Post a Comment