Errors in programming

Errors are problems or faults that occur in the program and cause unexpected behavior. Some errors don’t allow the program to begin execution, some can interrupt the execution, and others can be detected only after analyzing the output of the program. Errors are a way through which our machines tell us that they do not understand what we want them to do. It is the same as if people do not follow the rules and words of our language, we may not understand what they want to say or want us to do. Let’s explore the various types of programming errors and how they occur.

Syntax errors

Syntax errors are the most basic type of errors. They arise when Ruby is unable to understand a line of code based on the rules of the language. For example, 2k is neither a valid variable name nor a valid expression in Ruby, so it will return a syntax error.

Syntax errors are almost always fatal, which is to say that there is never a way to execute a piece of code containing syntax errors successfully. Sometimes, a syntax error doesn’t even allow the program to begin execution.

Press + to interact
variable = 5+ # Assigning an invalid value to the variable
print(variable)

The + written at the end of line 1 causes a syntax error because we cannot use an operator without it being followed by an operand. Possible fixes to resolve this error include:

  • Removing the symbol + if it’s a simple assignment
  • Adding a second operand after + if the variable should store the sum of two numbers

Logical errors

Most of the time, a program completes its execution despite an error in the code. When we can trace such errors due to the unexpected output of the program, it is called a logical error. A simple example of a logical error is demonstrated in the following program:

Press + to interact
a = 8
if a % 2 == 0 # If the number is divisible by 2
print("#{a} is an odd number\n")
else
print("#{a} is an even number\n")
end

Logical errors are only erroneous in the perspective of the programming goal. The if statement above works within the framework of Ruby, but gives the opposite of the expected result. We can fix this error by inverting the conditional expression or by swapping the true and false branches.

Exceptions

Exceptions arise when Ruby knows what to do with a piece of code but is unable to perform the action. For example, if Ruby attempts to access the Internet but no Internet connection is available, Ruby knows what to do with the command but is unable to perform it. Similarly, Ruby knows how to divide two numbers, but the division by zero is undefined. The following program demonstrates the same exception:

Press + to interact
a = 5
b = 0
print("#{a}\n")
print("#{b}\n")
result = a / b # Dividing a by b
print(result)

The program above executes and shows the output up to line 4. Division by zero gives us an undefined result. The divisor, b, in line 5 interrupts the execution of the program with an exception. We can fix this error by using a nonzero divisor.

Ruby error practice

Here are a few example programs to help us understand errors in Ruby.

Greater of two numbers

We want a program that shows the greater of two distinct numbers. There is an error in the following code. Let’s try to trace and fix it to obtain the desired output. If you need a hint, asking the AI mentor is a good idea.

Press + to interact
a = 1
b = 2
if a > b # If a greater than b
print("#{b} is greater than #{a}\n")
else
print("#{a} is greater than #{b}\n")
end

Multiple or non multiple

We want to write a program that prints either multiple or not multiple depending on whether the second number is a multiple of the first one or not. There’s an error in the following code. Let’s try to trace and fix it to obtain the desired output:

Press + to interact
first = 4
second = 8
if second / first == 0 # If first number is completely divisible by second
print("#{second} is a multiple of #{first}\n")
else
print("#{second} is not a multiple of #{first}\n")
end

The area and perimeter of a square

We want to write a program that calculates the area and perimeter of a square. There’s an error in the following code. Let’s try to trace and fix it to obtain the desired output.

Press + to interact
side = 5
print("The side of square is #{side}.\n")
perimeter = side * # Calculating perimeter
area = side * # Calculating area
print("Area of a square : #{area}\n")
print("Perimeter of a square : #{perimeter}\n")