Last Updated : 15 Jul, 2025
Errors are problems in a program that causes the program to stop its execution. On the other hand, exceptions are raised when some internal events change the program's normal flow.
Syntax Errors in PythonSyntax error occurs when the code doesn't follow Python's rules, like using incorrect grammar in English. Python stops and points out the issue before running the program.
Example 1 : In this example, this code returns a syntax error because there is a missing colon (:) after the if statement. The correct syntax requires a colon to indicate the start of the block of code to be executed if the condition is true.
Python
a = 10000
if a > 2999
print("Eligible")
Output
Syntax errorExample 2: This code returns a syntax error because parentheses around the condition in an if statement are not required in Python. While optional, their unnecessary use can lead to incorrect syntax.
Python
Output
Syntax error Python Logical Errors (Exception)Logical errors are subtle bugs in the program that allow the code to run, but produce incorrect or unintended results. These are often harder to detect since the program doesn’t crash, but the output is not as expected.
Characteristics of Logical ErrorsExample:
Python
a = [10, 20, 30, 40, 50]
b = 0
for i in a:
b += i
res = b / len(a) - 1
print(res)
Explanation: Expected output is that the average of a should be 30, but the program outputs 29.0. The logical error occurs because the formula b/ len(a) - 1 incorrectly subtracts 1, leading to an incorrect result. The correct formula should be b / len(a).
Common Builtin ExceptionsSome of the common built-in exceptions are other than above mention exceptions are:
Exception Description IndexError When the wrong index of a list is retrieved. AssertionError It occurs when the assert statement fails AttributeError It occurs when an attribute assignment is failed. ImportError It occurs when an imported module is not found. KeyError It occurs when the key of the dictionary is not found. NameError It occurs when the variable is not defined. MemoryError It occurs when a program runs out of memory. TypeError It occurs when a function and operation are applied in an incorrect type.Error HandlingNote: For more information, refer to Built-in Exceptions in Python
Python provides mechanisms to handle errors and exceptions using the try, except, and finally blocks. This allows for graceful handling of errors without crashing the program.
Example 1: This example shows how try, except and finally handle errors. try runs risky code, except catches errors and finally runs always.
Python
try:
print("code start")
print(1 / 0)
except:
print("an error occurs")
finally:
print("GeeksForGeeks")
code start an error occurs GeeksForGeeks
Explanation:
Example 2: This example shows how try and except handle custom errors. try checks a condition, raises a ValueError if it fails and except catches and prints the error message.
Python
try:
a = 1999
if a < 2999:
raise ValueError("please add money")
else:
print("Eligible")
except ValueError as e:
print(e)
Explanation:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4