
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Are Python Exceptions Runtime Errors?
Are Python Exceptions Runtime Errors?
Yes, Python exceptions are considered runtime errors. Most exceptions occur during runtime.
Exceptions in Python are errors that occur when there is an abnormal scenario during the execution of a program, which terminates the execution abruptly, interrupting its normal flow. Examples of exceptions are ZeroDivisionError, IndexError, ValueError, etc..
Whereas, errors in Python are detected before the execution of the program, they include syntactical errors and other violations of the program rules. Examples of errors are SyntaxError, IndentationError, etc.
Exceptions are Raised at Runtime
Exceptions occur when the execution of the program is interrupted because of issues like logical errors, incorrect data types, or issues with external resources, such as missing files or network failures.
Example: ZeroDivisionError at Runtime
In this example, the error occurs when the code is executed and tries to divide by zero -
a = 10 b = 0 try: result = a / b except ZeroDivisionError as e: print("Caught runtime error:", e)
Following is the output obtained -
Caught runtime error: division by zero
Errors are Raised at Compile Time
Errors occur during the parsing of the code, before the program runs. They consist of syntax errors like missing colons, unmatched brackets, or incorrect indentation, which prevent the code from being executed.
Example: SyntaxError Before Execution
Here, Python raises a SyntaxError because of the missing colon. This happens before execution begins -
# This code will fail to run if True print("Missing colon will cause syntax error")
We get the output as shown below -
File "/home/cg/root/682c9745587be/main.py", line 2 if True ^ SyntaxError: expected ':'
Example: IndentationError Before Execution
The following program never runs because the function body is not properly indented -
# This code also fails before running def greet(): print("Hello")
The error obtained is as shown below -
File "/home/cg/root/682c9745587be/main.py", line 3 print("Hello") ^ IndentationError: expected an indented block after function definition on line 2