One of most important aspect of any programming language is – How it deals with Errors? Usually whenever some programmer writes a piece of code there exists some chances of code raising Errors while executing it. If your code have Errors in it, then chances are high that without fixing these you will not be able to completely make a program. Whenever I work on a project of developing some program using Python, I usually do testing as I write code. This helps to identify source of Error easily and fix it.
But how does Python shows these Errors to programmer, its done by raising Exceptions. Its same like when you order something online and totally different product come to your home then you raise a complaint to online retailer. Similiarly if Python Interpreter is expecting a Tab in code and you instead have coded a Space then Interpreter will say Ohhh!! That’s wrong.
Let’s deep dive into Python Exception Class to see what kind of Errors can Python raise?
Python Exception Class
Python have a built-in exception class which is a Base-Class for all other Exception Classes in Python. Moreover if your defining some error catching procedure for class which you have defined yourself in Code, then its always recommended to inherit from built-in Base Class Exception.
Exception Class can raise two types of Errors in Python
- Syntax Errors – Raised when Python Interpreter sees that code is not properly formatted.
- Logical Errors – These errors are raised on run-time, when Python Interpreter tries to compute something which is not supported by Python Language itself like Zero Division.
Concrete Exceptions in Python – Table
|AssertionError||Raised when assert statment fails|
|AttributeError||Raised when an attribute reference or assignment fails|
|EOFError||Raised when input() function hits an end of file condition without reading any data|
|GeneratorExit||Raised when generator or coroutine is closed|
|ImportError||Raised when Python Interpreter either fails to load a module or when specified function to be imported doesn’t exist inside module itself.|
|ModuleNotFoundError||Raised when Python interpreter cannot locate a module|
|IndexError||Raised when you try to access some index of a Python Sequence which doesn’t exist|
|KeyError||When a key is not found inside Dictionary, this error is raised|
|KeyboardInterrupt||Usually raised when interrupt key(Control and C key or delete) is pressed|
|MemoryError||When run-time operations doesn’t left with enough memory for their execution|
|NameError||Raised when Python doesn’t find name of some identity(Set, list, Dictionary or something else)|
|NotImplementedError||Raised when exception handling code is not defined for class defined by programmer. Usually used for while writing classes for real time testing|
|OSError||Raised for System-related errors like when some file is not found by Python Interpreter|
|OverflowError||Raised when result of some arithmetic operation cannot be represented|
|RecursionError||Raised when Interpreter detects that maximum recursion depth has been reached|
|RuntimeError||Raised when interpreter found some error which it cannot classify as some Error type|
|StopIteration||Raised by built-in next() function when there are no further items being produced by the iterator|
|StopAsyncIteration||Must be raised by __anext__() method of an asynchronous iterator object to stop the iteration|
|SyntaxError||Raised when parser cannot figure out syntax of code|
|IndentationError||Raised when Python code is not properly indented|
|TabError||Raised when indentation of code does not have proper tabs/spaces|
|SystemError||Raised when some internal error in Python Interpreter occurs|
|SystemExit||Raised when Python Interpreter exits and return some status code|
|TypeError||Raised when an operation or function is applied to an object of inappropriate type|
|UnboundLocalError||Raised when a reference is made to a local variable in a function or method, but no value has been bound to that variable|
|UnicodeError||Raised when a Unicode-related encoding/decoding error occurs|
|UnicodeEncodeError||Raised when a Unicode-related error occurs during encoding|
|UnicodeDecodeError||Raised when a Unicode-related error occurs during decoding|
|UnicodeTranslateError||Raised when a Unicode-related error occurs during translating|
|ValueError||Raised when an operation or function receives an argument that has the right type but an inappropriate value|
|ZeroDivisionError||Raised when the second argument of a division or modulo operation is zero|
Operating Systems Exceptions in Python – Table
|BlockingIOError||Raised when an operation would block on an object (e.g. socket) set for non-blocking operation|
|ChildProcessError||Raised when an operation on a child process failed|
|ConnectionError||Connection related errors|
|BrokenPipeError||Raised when trying to write on a pipe while the other end has been closed, or trying to write on a socket which has been shutdown for writing|
|ConnectionAbortedError||Raised when a connection attempt is aborted by the peer|
|ConnectionRefusedError||Raised when a connection attempt is refused by peer|
|ConnectionResetError||Raised when a connection is reset by the peer|
|FileExistsError||Raised when trying to create a file or directory which already exists|
|FileNotFoundError||Raised when a file/directory is requested but doesn’t exist|
|InterruptedError||Raised when a system call is interrupted by an incoming signal|
|IsADirectoryError||Raised when a file operation is requested on a directory|
|NotADirectoryError||Raised when a directory operation is requested on something which is not a directory|
|PermissionError||Raised when trying to run an operation without the adequate access rights|
|ProcessLookupError||Raised when a given process doesn’t exist|
|TimeoutError||Raised when a system function timed out at system level|
Warning Exceptions in Python – Table
|DeprecationWarning||Base class for warnings about deprecated features when those warnings are intended for other Python developers|
|PendingDeprecationWarning||Base class for warnings about features which are obsolete and expected to be deprecated in the future, but are not deprecated at the moment|
|SyntaxWarning||Base class for warnings about dubious syntax|
|RuntimeWarning||Base class for warnings about dubious runtime behavior|
|FutureWarning||Base class for warnings about deprecated features when those warnings are intended for end users of applications that are written in Python|
|ImportWarning||Base class for warnings about probable mistakes in module imports|
|UnicodeWarning||Base class for warnings related to Unicode|
|BytesWarning||Base class for warnings related to bytes and bytearray|
|ResourceWarning||Base class for warnings related to resource usage|
Exception Class Hierarchy in Python
BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception +-- StopIteration +-- StopAsyncIteration +-- ArithmeticError | +-- FloatingPointError | +-- OverflowError | +-- ZeroDivisionError +-- AssertionError +-- AttributeError +-- BufferError +-- EOFError +-- ImportError | +-- ModuleNotFoundError +-- LookupError | +-- IndexError | +-- KeyError +-- MemoryError +-- NameError | +-- UnboundLocalError +-- OSError | +-- BlockingIOError | +-- ChildProcessError | +-- ConnectionError | | +-- BrokenPipeError | | +-- ConnectionAbortedError | | +-- ConnectionRefusedError | | +-- ConnectionResetError | +-- FileExistsError | +-- FileNotFoundError | +-- InterruptedError | +-- IsADirectoryError | +-- NotADirectoryError | +-- PermissionError | +-- ProcessLookupError | +-- TimeoutError +-- ReferenceError +-- RuntimeError | +-- NotImplementedError | +-- RecursionError +-- SyntaxError | +-- IndentationError | +-- TabError +-- SystemError +-- TypeError +-- ValueError | +-- UnicodeError | +-- UnicodeDecodeError | +-- UnicodeEncodeError | +-- UnicodeTranslateError +-- Warning +-- DeprecationWarning +-- PendingDeprecationWarning +-- RuntimeWarning +-- SyntaxWarning +-- UserWarning +-- FutureWarning +-- ImportWarning +-- UnicodeWarning +-- BytesWarning +-- ResourceWarning
Well as of now, if you have gone through whole of article you may have become familiar with different types of Exception Errors in Python. But as a programmer the biggest challenge is to find ways to solve these Errors because for most of these errors, names itself speak its meaning like TypeError related to something with different types. (Quite Simple)
How to solve Exception Errors in Python? If in case your total beginner then it would be really difficult to solve these errors by self, so I would recommend using Google and StackOverFlow for finding out solutions for these errors. Also as you get to solve more and more of these errors, your skill of pinning down to source of Error and then knocking it out of code will improve.
That’s my personal experience, I still remember writing my first piece of Python Code and when running it seeing Syntax error 😰😥it almost took me an hour to find why error is coming up and then only I got a way to get rid of this Syntax error.
The more you write code, the better you become.
Happy Coding 🥳