Exeception in python

What is Exception?

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. In general, when a Python script encounters a situation that it can't cope with, it raises an exception. An exception is a Python object that represents an error.
When a Python script raises an exception, it must either handle the exception immediately otherwise it would terminate and come out.

Handling an exception:

If you have some suspicious code that may raise an exception, you can defend your program by placing the suspicious code in a try: block. After the try: block, include an except: statement, followed by a block of code which handles the problem as elegantly as possible.

Syntax:

Here is simple syntax of try....except...else blocks:
try:
   You do your operations here;
   ......................
except ExceptionI:
   If there is ExceptionI, then execute this block.
except ExceptionII:
   If there is ExceptionII, then execute this block.
   ......................
else:
   If there is no exception then execute this block. 
Here are few important points about the above-mentioned syntax:
·         A single try statement can have multiple except statements. This is useful when the try block contains statements that may throw different types of exceptions.
·         You can also provide a generic except clause, which handles any exception.
·         After the except clause(s), you can include an else-clause. The code in the else-block executes if the code in the try: block does not raise an exception.
·         The else-block is a good place for code that does not need the try: block's protection.

Example:

Here is simple example, which opens a file and writes the content in the file and comes out gracefully because there is no problem at all:
#!/usr/bin/python
 
try:
   fh = open("testfile", "w")
   fh.write("This is my test file for exception handling!!")
except IOError:
   print "Error: can\'t find file or read data"
else:
   print "Written content in the file successfully"
   fh.close()
This will produce the following result:
Written content in the file successfully

Example:

Here is one more simple example, which tries to open a file where you do not have permission to write in the file, so it raises an exception:
#!/usr/bin/python
 
try:
   fh = open("testfile", "w")
   fh.write("This is my test file for exception handling!!")
except IOError:
   print "Error: can\'t find file or read data"
else:
   print "Written content in the file successfully"
This will produce the following result:
Error: can't find file or read data

The except clause with no exceptions:

You can also use the except statement with no exceptions defined as follows:
try:
   You do your operations here;
   ......................
except:
   If there is any exception, then execute this block.
   ......................
else:
   If there is no exception then execute this block. 
This kind of a try-except statement catches all the exceptions that occur. Using this kind of try-except statement is not considered a good programming practice though, because it catches all exceptions but does not make the programmer identify the root cause of the problem that may occur.

The except clause with multiple exceptions:

You can also use the same except statement to handle multiple exceptions as follows:
try:
   You do your operations here;
   ......................
except(Exception1[, Exception2[,...ExceptionN]]]):
   If there is any exception from the given exception list, 
   then execute this block.
   ......................
else:

   If there is no exception then execute this block.