Skip to main content

A Typical Guide to Exceptions in Python

In this blog, I will give you the basic but important information regarding the Exceptions in Python.

As we all know, we always get some Errors and Exceptions in program every now and then, so to know what that exception is all about. We should know all about exception.

So, let's dive into it.

First of all, let's understand what Exception is?

If a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. This errors detected during execution are called Exceptions.

Example:- 




Exceptions are of different types and this error message indicates what happened.
Like you see here in example i.e. ZeroDivisionError, NameError, TypeError. These are basic errors in the program.

List of Standard Exception :-
  1. Exception - Base Class for all the exception

  2. Stop Iteration - Raised when the next() method of an iterator does not point to any object.
    Example :-


  3. System Exit - Raised by the sys.exit() function.

  4. Standard Error - Base Class for all the built-in exceptions except Stop Iteration and SystemExit.

  5. ArithmeticError - Base Class for all errors that occur for numeric calculation.

  6. OverFlowError - Raised when a calculation exceeds a maximum limit for a numeric type.
    Example:-


  7. FloatingPointError - Raised when a floating point calculation fails. Floating point exception handling is not standardized, so floats are not checked. Regular integer are converted to long values are needed.

  8. ZeroDivisionError - Raised when division or modulo by zero takes place for all numeric types.
    Example:-


  9. AssertionError - Raised in case of failure of the Assert Statement.
    Example :-


  10. AttributeError - Raised in case of failure of attribute reference or assignment.
    Example :-

These are some of the exceptions that generally occurred during the programming. There are some other exception that we will cover in our next post.

Hope this help, guys. Stay tune for next blog.

Comments

Popular posts from this blog

Getting error while installing Scipy using pip3 for 64 bit version of Python ? Here's the solution!

As you know Scipy is one of the most used modules by the Python programmers for Data science and Machine Learning. So if you have ever try to install Scipy using pip3 (3 for Python 3!), you will get an error like this. As you can see, to install Scipy , simple pip3 install scipy  will not work. To install Scipy, you have to go to Unofficial Windows Binaries for Python Extension Packages  link. Then you have to find for Scipy. If you are using Chrome, you can use cntrl + F to search for scipy. I m using python 3.5 (64-bit) so I have downloaded this package. Make sure to install numpy before scipy otherwise this method wont work. To install numpy, simple pip3 install numpy   won't work . You have to download numpy + mkl from the same link and download it, then install it using pip. To install numpy and scipy wheel file you have to use pip  install  <path of the wheel file> As you can see,pip has...

A typical guide to use virtual environment for your python projects.

Virtual Environment is used to create isolated python environment. In simple terms it creates a virtual installation of Python and Pip into some directory. It is separated from your original python installation. Why we need virtual environments? Suppose you are working on some project and that project required more than 2 libraries that are dependent on each other. Generally these libraries are open sourced projects and maintained by different developers community. Now, if any one library gets changed in newer version and it is not backwards compatible and if you accidentally update that library from your global python installation then it might break your project.That's why it is safe to use virtual environments. To create virtual environment in your machine please follow these steps: Make sure you have installed python in your computer.In this guide I will assume you have installed python 3.(make sure to check add to path) Install virtual env via this command $ pip i...

How to use pip for Python 3 alongside Python 2?

pip  is a package management system used to install and manage software packages written in  Python .You can find many third party packages on Python Package Index(PyPI).  There are currently   94060  packages here.You can browse all of the packages at the following link  PyPI packages So to use pip for Python 3 alongside Python 2, Follow this steps: Step 1: First of all, you have to install both python versions.If you haven't installed it already you can see my other post how to install python 2 and python 3 on Windows PC.                                           Make sure to check   Add python 3.5 to path while installing Python 3. Step 2: After installation, you have to setup your path for pip  (for Python 2) in your windows PC. To do that first of all copy the following path  C:\Python27\Scripts ...