Back to Home

A Quirk of Programming a Calculator

The Importance of Exceptions

Programming a calculator is mainly a simple task, a basic one primarily consists of
taking two number variables and performing an operation on them. However,
A quirk of math means you have to take into consideration an extra step when making one
That consideration is dividing by 0. If you do not account for this you can cause your calculator
to break in unforeseen ways.


Here is an example of some code written with an exception for divide by 0.
        
        if (operator == '/'){
          if (num2 == 0){
            System.out.println("Error, can't divide by 0.");
          } else
          result = num1 / num2;
        }
        
      

Professor Greatest Programmer has famously said:It is vital to make an exception for divide by 0 to avoid logical errors.
-Basics of Programming Math