Photo by Sigmund on Unsplash

Errors!

Kalidas

If you have even once programmed in your life the one thing you might have encountered even before a successful “hello world” output those are those inevitable things every programmer hates known as bugs.

Bugs are not the actual bugs you find in real life but is a term given to Errors that occur during the coding process. Errors are basically mistakes or faults that can occur in the code while programming due to a variety of reasons which might lead to the code not running properly, producing incorrect outputs and even the code not running at all.

So Why “Bugs”

Photo by Ugi K. on Unsplash

Well you might be wondering why bugs and not any other name , the story of the origin of errors being called bugs is quiet interesting. The term “bug” originated back in the 1950s when an error in the mark1 was linked to a moth which was found out in the computer’s relay. Grace Hopper a computer pioneer actively used and publicized the term bug for coding errors and the process of removing error/bugs as debugging.

There are many different types of errors out there and as a programmer it is necessary to learn about such errors ,as knowing them makes the debugging process more productive . There are basically three types of errors and all the other error that are seen comes under any of the three errors mentioned below.

So lets head on to it.

Compile time error
Well from the name of it, anyone can guess that this error occurs during compilation, basically compile-time errors are those types of errors that can be caused due by a variety of reasons with one major reason being known as Syntax Errors which are basic errors made in the grammar of the specific language such as incorrect usage of keywords, wrong usage of the datatype, incorrect declarations of variables and functions, spelling mistakes and punctuation errors, etc. Compile time errors are comparatively easy to solve as the errors get detected at the start itself and basically, syntax errors are easy to debug because they are straightforward and easy to spot but a good understanding of the specific language is a must.
In the below example the brackets are used incorrectly which will lead to a compile-time error. This is referred to as a syntax error.

print("hello world"}

Logical Error
This is unlike the previous errors that you have seen, a code that does not have compile-time or runtime errors can still not be error-free! Yes, you might get confused at first but these are those errors that give an output but the output need not be correct. For getting a better understanding of this error take this example, if the execution of a code produces an output of 10 we find that the code is free of errors and is successfully run but the actual output is 100 in such a scenario there is a logical error. As the name suggests this error comes out of an incorrect logic in the code which can only be solved after a thorough examination of the code unlike the previous errors the IDE or language might not give a description of the error but it is solely up to the programmer to solve it as this relies completely on the logic of code.

In the below code the developer has to find the average from “numbers” variable of type list but here instead of dividing the sum by the length of list it is divided by 2 which won’t produce any errors but will generate false results.

def calculate_average(numbers):
if not numbers:
return 0

total = sum(numbers)
average = total / 2
return average

Runtime error
Well you might be familiar what is run time(the time period in which code gets executed up until termination). The errors which occurs during runtime is known as runtime errors or exceptions. If a runtime error is generated it basically means that the code is free from compile time errors and the errors are produced during the runtime which can be due to a variety of reasons such as, if the code produces an infinite loop, division by zero, index out of bounds error, file not found exception, etc. Such errors are hard to detect during compile time as the syntax of the code and many other factors affecting the compilation phase is correct. If runtime errors are not handled properly it can lead to the program crashing which the end user must not experience.

In the below code snippet there is an attempt to divide a value by zero. The code won’t produce any compile time errors but during runtime when the division occurs the error is generated in most programming languages this is known as a “ZeroDivisionError”.

result = 10 / 0  # This will raise a ZeroDivisionError

No responses yet

Write a response