Python, Tutorials

Exception Handling in Python

Every now and then ,while programming, we are bound to come across certain errors which can be syntaxial or an exception.

While syntax error is detected by the parser before the script runs, exceptions do not restrict the script to run but disrupts the normal flow of the program. 

Syntax error can be solved by using Linting before the runtime, but exceptions occur only when the programs run so it can only be observed while trial and error runs.

In a nutshell, exception handling can be defined as the process of detecting, identifying and handling exceptions. These errors or metaphorical roadblocks are collectively referred to as exceptions. One thing to note is that exceptions may not always be fatal.

Even the most sophisticated and high-end program codes are prone to raising exceptions if not handled correctly.  In Python programming, exception handling is one of the fundamentals every developer should learn and implement to make robust programs.

Example of Errors:

  1. Zero Division Error: Suppose you made a calculator program and it runs successfully. The user wants to divide 71 by 0. As soon as the user tries to divide 71 by 0 your program crashes and throws an error “ZeroDivisionError: division by zero” which we all know is not possible.
>>> 71/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero

2. Name Error: Raised when a local or global name is not found. This applies only to unqualified names. The associated value is an error message that includes the name that could not be found.

4 + spam*3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'spam' is not defined

3. Type Error: Raised when an operation or function is applied to an object of inappropriate type. The associated value is a string giving details about the type mismatch.

>>> '2' + 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly

You can read more about built-in exe python-docs.

How to deal with exceptions and resolve it.

The try-except combo

The try-except duo is perhaps the most commonly used exception handling technique. The user can mention the operation to be performed under the  “try “ statement. In the case of any exception, the required course of action which is to be followed, is mentioned under the “except” statement.

The use of the else clause is better than adding additional code to the try clause because it avoids accidentally catching an exception that wasn’t raised by the code being protected by the try … except statement.

When an exception occurs, it may have an associated value, also known as the exception’s argument. The presence and type of the argument depend on the exception type.

The except clause may specify a variable after the exception name.

The raise statement allows the programmer to force a specified exception to occur.The sole argument to raise indicates the exception to be raised. This must be either an exception instance or an exception class .The finally clause will execute as the last task before the try statement completes. The finally clause runs whether or not the try statement produces an exception.

Want to learn more in depth read errors in python docs


Thank you for reading, Happy Learning, drop your suggestion in the comments.

Feel free to follow us on Youtube, Linked In , Instagram