Python Errors and Exceptions
Encountering errors and exceptions can be very frustrating at times, and can make coding feel like a hopeless endeavour. However, understanding what the different types of errors are and when you are likely to encounter them can help a lot.
Python Errors can be of three types:
- Compile time errors (Syntax errors)
- Runtime errors (Exceptions)
- Logical errors
Compile time errors (Syntax errors)
Errors caused by not following the proper structure (syntax) of the language are called syntax or parsing errors.
Runtime errors (Exceptions)
Exceptions occur during run-time. Your code may be syntactically correct but it may happen that during run-time Python encounters something which it can't handle , then it raises an exception.
Logical errors are the most difficult to fix. They occur when the program runs without crashing, but produces an incorrect result. The error is caused by a mistake in the program's logic . You won't get an error message, because no syntax or runtime error has occurred.
If you encounter an error and don't know what it means, it is still important to read the traceback closely . That way, if you fix the error, but encounter a new one, you can tell that the error changed. Additionally, sometimes knowing where the error occurred is enough to fix it, even if you don't entirely understand the message. If you do encounter an error you don't recognize, try looking at the official documentation on errors .