Most of the time, there is skepticism between errors are exceptions when it comes to a programming language. So, this article will help you know about and differentiate between errors and exceptions in Python.
In the article we’ll learn about:
- What are errors?
- Types of errors
Errors and Exceptions both distort a program’s execution. They are similar but not the same concepts. Error is some flaw in your code that stops the compilation of the code and hence doesn’t allow the same code to run.
For example,

There are 3 types of errors.

1. Compile-Time Errors
As the name suggests, the errors that occurs while compilation or at the compile-time are called compile-time errors.
Now, you must be wondering, how errors occur at compile time?
Answer to the question is; At the time of compilation, the system checks the source code. Now if there’s any fault or violation of programming convention or you can say violation of language’s rules then compilation stops and system gives us compilation error.
There are 2 types of compile-time errors.
Syntax Errors
Syntax refers to the formal rules those are used for writing valid statements in a programming language. You can understand it as a structure of the code and conventions that you have to follow to construct that structure.
For example,

In the above example, we can see a syntax error. We know that in Python; the syntax for if statement is:
if(condition):
In the example “:” was missing, hence the compilation stopped and there was an error. The correct code would be:
n = int(input("Enter a number:"))
if(n<5):
print(n)
NOTE
The example had only 3 lines of code, but when we are working on huge projects, the line of code increases it could be in thousands, and finding such small errors like- “;”, “:” could be difficult. Also, this wastes computing and programmer’s time and money too. So, since it is preventable, we should try to get the syntax right the first time.
Semantics Errors
Semantics refers to the meaning of a statement. So, when a statement doesn’t make any sense and is not meaningful then we can say it is a Semantic error.
For example,

In the above example, we can see a syntax error but actually it is a semantic error, because the statement:
sum+n=sum
doesn’t make any sense. This is because an expression cannot come on the left side of an assignment according to Python language’s rule.








Join the conversation! Your thoughts help the community grow.