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

Python 2 vs Python 3 Pystone benchmarking test!

When Python 3 came out, It was slower than Python 2. As Python's official blog says Python 3 is 10% slower than Python 2. So it means Python 2 is faster than python 3 by some margin. In this Benchmarking, we will use Pystone benchmarks to compare Python 2 and Python 3. My system specification is listed below: CPU = Intel core i3 2100 @ 3.10 GHZ GPU = Nvidia Geforce 210 RAM = 2 GB OS = Windows 10 Pro The benchmarking results may vary upon your system specification. Here's the result. python 3 pystone benchmarks python 2 pystone benchmarks As you can see python 2 has 24% higher benchmarks than python 3 which is pretty impressive. Maybe this is one of the reasons that some programmers prefers to use Python 2 instead of Python 3. Hope this helps :)

Want to use python 2 and 3 both on your windows pc? Here we go!

Step 1: First of all, you have to download both python 2 and 3 setup files.I am using Windows machine and so I have downloaded both python 2.7.12 and python 3.5 which are the latest version of python 2 and 3 respectively. (I recommend you to download 64-bit installer for both versions) Step 2: First, install python 2 and then python 3.Before installing python 3, make sure to check the box of Add python 3.5 to path as shown below. How to open both python shell in command prompt : If you want to use python shell in command prompt, you have to type the following as you can see in image below. How to open both python file in specified python version: If you want to open a file automatically by double clicking the .py file then you should specify which python version you are using.Then you should type the first line of python program as #!python2 and #!python3 for python 2 and python 3 as shown below. py2.py py3.py If you run the py2.p...