Friday, March 22, 2013

Exception Handling in Constructor


Constructor can have Exception parameter which will be declared at the time of declaration and instantiate at the time of creating the object. Exception parameter will take a constant value and sy-subrc will return that constant value if the exception raises.

Here is a program which gives the output of Factorial value. The constructor method import the input parameter and has an exception parameter. In the method we declare the condition that if the input parameter is greater than 10 then exception will be raised otherwise the Factorial function will work. After the create object we check the value of sy-subrc and if exception raises then sy-subrc will return the constant value which is instantiated to exception parameter.

REPORT  zsr_test.

PARAMETERS p_data TYPE i.

*----------------------------------------------------------------------*
*       CLASS cl_construct DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_construct DEFINITION.
  PUBLIC SECTION.
    METHODS constructor IMPORTING  i_data TYPE i
                        EXCEPTIONS exc.
ENDCLASS.                    "cl_construct DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_construct IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_construct IMPLEMENTATION.
  METHOD constructor.
    IF i_data GT 10.
      RAISE exc.
    ELSE.
      WRITE: / i_data, ' is less than 10'.
    ENDIF.
  ENDMETHOD.                    "constructor
ENDCLASS.                    "cl_construct IMPLEMENTATION

START-OF-SELECTION.
  DATA obj TYPE REF TO cl_construct.
  CREATE OBJECT obj EXPORTING i_data = p_data EXCEPTIONS exc = 1.

  IF sy-subrc <> 0.
    WRITE: / p_data, 'is greater than 10, so SY-SUBRC = ', sy-subrc.
  ENDIF.

Below is the Output:


Exception Raised:


Otherwise:


9 comments:

simashree said...

Thank you for such guidance about astrology,Best Blog on
vasiyam in chennai

Bhavya Kumar said...

Best blog.Got to learn new things.Thanks for this Blog SAP Success Factors Training in Chennai

kumar prodip said...

Sir its very good site ever i have seen. i request to you sir please give some example using with standard table & fields.

Sasi S said...


SAP SuccessFactors Training in Chennai


Best SAP Success Factors Training Institutes in Chennai by Leading HR Company.Real Time SAP SF

Training by Mr.Karthick.

Contact No: 9003085882


http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

We also provide this SAP Successfactors Training in Vadapalani,Velachery,ECR,Adyar,Chromepet,Pallavaram & Guindy.

Class Room / Online Training Availabale!

sanayamohit said...

SAP Success Factors Real Time Hands on Training in Chennai...

Don't always Depend on Training Institute Alone and so please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer over SAP Success Factors Means??? Please ready for an DEMO From the Trainer MR.Karthick
CONTACT:8122241286

Both Classroom/Online Training is Available!!!!!!

Shruti Ramalingam said...

From My search…Creating Experts provides Best SAP Training with real time projects assistance.

Most of the modules are equipped with advance level topics which the student can learn from the basics to the advance level stage.

They also provide placement assistance in leading MNC companies across the globe according to the current requirements.

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

And these are the Best SAP training institute which provides Real Time Hands on Training…

codedion Technologies-9003085882

creating Experts-8122241286

http://thecreatingexperts.com/sap-mm-training-in-chennai/

Unknown said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!

Keerthi Suresh said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!

gilledashivani said...

Informative article. Thank you for sharing the great blog. You have done great explanation and visualization. Keep up the good work.
BEST SAP ABAP TRAINING IN HYDERABAD