Global class & Exception Class Creation
Transaction: SE24
Through this transaction code, we can able to create a global class. Basically global class are created for the use of reusability. If reusability is not not required, then we can create a local class.
Lets class name starts with YCL_ or ZCL
Provide a description for the class and make as usual ABAP Class with Public.
Final methods cannot be redefined in subclasses. Final classes cannot have any more subclasses and constitute the final node of an inheritance tree.
Lets create a sample program using this global class, for this purpose we have created an attribute and few methods with an exception class.
Properties Tab
Attributes Tab
Here, I have created an attribute (A_SFLIGHT), which can used for all the methods in the same class.
Methods Tab
Here I have created a CONSTRUCTOR method, Constructors are special methods that cannot be called using CALL METHOD. Instead, they are called automatically by the system to set the initial state of a new object or class. There are two types of constructors - instance constructors and static constructors. Constructors are methods with a predefined name.
The system calls the instance constructor once for each instance of the class, directly after the object has been created in the CREATE OBJECTstatement. You can pass the input parameters of the instance constructor and handle its exceptions using the EXPORTING and EXCEPTIONS additions in the CREATE OBJECT statement.
The static constructor of a class is the predefined static method CLASS_CONSTRUCTOR.
The static constructor has no parameters. The system calls the static constructor once for each class, before the class is accessed for the first time. The static constructor cannot therefore access the components of its own class.
In constructor method
In above mentioned code, I have used the exception class. Creation of Exception class are explained finally.
Method VALIDATE_SFLIGHT
Method COST_OF_FLIGHT
Usage of global class in a report program
*---* Reference Object...
DATA : lr_object TYPE REF TO ycl_sample_class_01,
lr_exception TYPE REF TO ycx_exec_sample.
*---* Structure...
lr_exception TYPE REF TO ycx_exec_sample.
*---* Structure...
DATA : x_sflight TYPE sflight.
*---* Variable Declaration...
DATA : v_error TYPE string,
v_price TYPE s_price,
v_curre TYPE s_currcode.
START-OF-SELECTION.
TRY.
CREATE OBJECT lr_object " Constructor
EXPORTING
carrid = 'AB'
connid = '17'
fldate = '20130828'.
lr_object->validate_sflight( IMPORTING sflight = x_sflight ). " Validate the sflight entry
lr_object->cost_of_flight( " Cost of the flight ticket
IMPORTING
price = v_price
currency = v_curre ).
CATCH ycx_exec_sample INTO lr_exception.
v_error = lr_exception->if_message~get_text( ).
WRITE v_error.
v_price TYPE s_price,
v_curre TYPE s_currcode.
START-OF-SELECTION.
TRY.
CREATE OBJECT lr_object " Constructor
EXPORTING
carrid = 'AB'
connid = '17'
fldate = '20130828'.
lr_object->validate_sflight( IMPORTING sflight = x_sflight ). " Validate the sflight entry
lr_object->cost_of_flight( " Cost of the flight ticket
IMPORTING
price = v_price
currency = v_curre ).
CATCH ycx_exec_sample INTO lr_exception.
v_error = lr_exception->if_message~get_text( ).
WRITE v_error.
ENDTRY.
END-OF-SELECTION.
WRITE : / 'Carrid :', x_sflight-carrid.
WRITE : / 'Price :', v_price.
WRITE : / 'Currency :', v_curre.
WRITE : / v_error.
END-OF-SELECTION.
WRITE : / 'Carrid :', x_sflight-carrid.
WRITE : / 'Price :', v_price.
WRITE : / 'Currency :', v_curre.
WRITE : / v_error.
Exception Class
Again use the Transaction code SE24 for creating a exception class
Let class name starts with YCX_ or ZCX
Follow the same procedure as per the below mentioned screenshot, to create the same.
In ATTRIBUTES Tab, I have added a attribute 'CARRID', to get the CARRID value while exception is raised.
In TEXT Tab, add the custom text messsage to get displayed while exception is raised.
Use this exception class, In the constructor method of YCL_SAMPLE_CLASS_01 class.
Lets see the output
With correct CARRID Value
With Incorrect CARRID Value
No comments:
Post a Comment
Note: only a member of this blog may post a comment.