Execeptions in Python: What they mean..
Errors arise even from a syntactically correct statement and The error that is detected during the execution of a statement is called an exception. In python, there are some, awesome error handling techniques but before it can be learnt, it is important to know the different types of errors that can occur.
Python has a wide range of built-in exceptions that are defined in the exceptions module. This module is implicitly imported while running python and it helps you to narrow down the type of error that has occurred.
Here is a sample screenshot of two errors:
A traceback is a module ,which works with the call stack and helps producing errors. It acts as a stack trace to locate the error causing line and reruns that line value.The last line of the exception messages is the name of the built-in exception and a small description as to what that error is and sometimes, tells which part of the statement is causing the error.
here are a few built-in exceptions I have come across:
- ValueError : This error occurs generally when the data to a built-in function or operation is not a correct value. This means that the error occurs when a value supplied is larger than the maximum value that can be processed or in some cases if the value is smaller than zero.
- ImportError : This error is raised when a module or a library that is not present is attempted to be imported into python. For example PIL is an imaging library in python and doesn’t come pre-installed with python3.2 . So if I try to import that library I get an import error. When this error is raised,it doesn’t always mean that the library or module isn’t there.It can simply mean that the module name might not be spelt correctly or the module isn’t installed yet.
- NameError : This is one of those errors that occur a large number of times. It is raised when there is an attempt to use a variable that has never been assigned a value.
- ZeroDivisionError : Basic mathematics tells us that a division by a zero is undefined. And when such an attempt is made, python raises this error. It can be due to directly specifying a division with zero like : >>>10 * (15/0)
or a result leading to a zero in the denominator Eg: >>> 10 * 45/(20-2 * 10) - AttributeError : Tuples in python are like lists but once created, they are immutable. And when there is an attempt to delete or add contents into it, this error is raised.
- KeyError: key is an unique entity of the dictionary data structure and also used to keep track of elements in a set. This error is raised when a key mentioned in a dictionary is not present or an attempt is made to remove elements from a set.
- IOError : this is a common error during file handling. This occurs when we try to print something or try opening a file or perform operations on file fails. The sources for these might be the file doesnt exist or the disk is full
- EOFError : This error is raised when a built-in function like input() reaches end-of-file before any input is given.
Other than these ,There are a lot more and can be found on the python online documentation.
But as of now with this knowledge on errors, learning exception handling will be much easier.