• Nirav M
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Following is my Apex class:

public class Calculator {
    
    public Decimal Operand1 {get;set;}
    public Decimal Operand2 {get;set;}
    public Decimal Result   {get;set;}
    public String errMessage {
        get { return errMessage;}
        set { errMessage='No Error';
              system.debug('in setter errMessage: '+ errMessage);}
    }
    
    public  Void Addition (){
        Result=Operand1+Operand2;
            //errMessage=null;
            }
    
    public  Void Substraction (){
        Result=Operand1-Operand2;
            //errMessage=null;
            }    
    
    public  Void Multiplication (){
        Result=Operand1*Operand2;
            //errMessage=null;
            }
    
    public  Void Division (){
        if (Operand2 != 0 ){
            Result=Operand1/Operand2;
            //errMessage=null;
            }
        else
        {   
            Result=Null;
            errMessage= ' Operand can not be Zero';
            system.debug('in Action method errMessage: '+ errMessage);
         }
    }
}

And Following is from Anonymous block to run the above class.
---------------------------------------------------------------
Calculator Calc = new Calculator();
Calc.Operand1=5;
Calc.Operand2=0;
Calc.Division();
system.debug('Final Result: ' + Calc.Result);
system.debug('Final errMessage: ' + Calc.errMessage);
--------------------------------------------------------

Why I don't see the error message "Operand can not be Zero" when i run the above.  
Following is my Apex class:

public class Calculator {
    
    public Decimal Operand1 {get;set;}
    public Decimal Operand2 {get;set;}
    public Decimal Result   {get;set;}
    public String errMessage {
        get { return errMessage;}
        set { errMessage='No Error';
              system.debug('in setter errMessage: '+ errMessage);}
    }
    
    public  Void Addition (){
        Result=Operand1+Operand2;
            //errMessage=null;
            }
    
    public  Void Substraction (){
        Result=Operand1-Operand2;
            //errMessage=null;
            }    
    
    public  Void Multiplication (){
        Result=Operand1*Operand2;
            //errMessage=null;
            }
    
    public  Void Division (){
        if (Operand2 != 0 ){
            Result=Operand1/Operand2;
            //errMessage=null;
            }
        else
        {   
            Result=Null;
            errMessage= ' Operand can not be Zero';
            system.debug('in Action method errMessage: '+ errMessage);
         }
    }
}

And Following is from Anonymous block to run the above class.
---------------------------------------------------------------
Calculator Calc = new Calculator();
Calc.Operand1=5;
Calc.Operand2=0;
Calc.Division();
system.debug('Final Result: ' + Calc.Result);
system.debug('Final errMessage: ' + Calc.errMessage);
--------------------------------------------------------

Why I don't see the error message "Operand can not be Zero" when i run the above.