Skip to main content

What are Errors?

In programming, an error (commonly known as a "bug") is a mistake in your code that prevents the program from behaving the way you expect. No matter how experienced a developer is, writing errors is a natural and inevitable part of coding.

When Python runs into an error, it stops executing your code and displays a traceback message (error report) to help you locate and fix the issue.


The Two Main Categories of Errors

In Python, errors are broadly classified into two major categories depending on when they are detected:

  1. Syntax Errors: Mistakes in writing code rules (the grammar of Python). These are detected before the program starts running.
  2. Runtime Errors (Exceptions): Mistakes that occur while the program is running. The grammar of the code is correct, but Python cannot execute a specific line due to invalid actions.

Comparison: Syntax Errors vs. Runtime Errors

To make it easy to understand the difference, let us compare them:

FeatureSyntax ErrorsRuntime Errors (Exceptions)
When does it happen?Before the program runs (Compile-time/Parsing stage).During the execution of the program.
Does the code start running?No. Python refuses to run even a single line.Yes. The program runs until it hits the line with the error.
Why does it happen?Typo in keywords, missing punctuation (colons, quotes, brackets).Division by zero, missing files, invalid type conversions.
Exampleif x = 5: (using = instead of ==)10 / 0 (cannot divide by zero)

The Concept of Exceptions

In Python, runtime errors are formally called Exceptions. When a runtime error occurs, Python is said to "raise an exception". If we do not write code to catch and handle this exception, the program will crash immediately.

In the next sections, we will explore both Syntax Errors and Runtime Errors in deep detail with clear code examples.