Python Exception Handling

PREV    NEXT

1. Exception Handling is the mechanism it is allows to handle errors very smartly while the program is running.
2. Runtime erros is nothing but it happens while running the program,if it is runtime error it will no throughs any sytax of the program

Some of the common Exceptions:

 1. IOError:It will happen when Input or output operation failed
2. EOFError:It will happen when the file has reached end point and still operations are goingon.
3. NameError:It will occur when name is not found.
4. ZeroDivisionError:It will happen a number is division by zero
5. IndentationError:When source code is not in indenet

Syntax:

1. Single Exception Handling:

try:

            suspicious code

except exception:

            exception statement

2. Multiple Exceptions:

try:

            suspicious code

except exception1:

            exception1 statement

except exception2:

            exception2 statement

except exception3:

            exception3 statement

 3. Try—exception with else clause

A try—exception statement also can have an optional else clause ,it is excute only when no exception has been occured.

Syntax:

try:

            suspicious code

except exception1:

            exception1 statement

except exception2:

            exception2 statement

except exception3:

            exception3 statement

else:

            statement

4. Try—exception with finally clause

A try—exception statement also can have an optional finally clause ,it is always excute wheathe  exception has been occured or not.

Syntax:

try:

            suspicious code

except exception1:

            exception1 statement

except exception2:

            exception2 statement

except exception3:

            exception3 statement

finally:

            statement

5. Raise an exception

We can explicitly raise an exception by usng raise statement ,it will be cause an exception to occur and it will not handle in case of execution control will stop.

Syntax:

            raise exception_class,data

Example1: 

      Iteration1                                                                 Iteration2

         Input:                                                                           Input: 


COMPILE & RUN

         Output:                                                             Output:

Example2:

try:

 

Iteration1                                                                   Iteration2

Input:                                                                          Input:

COMPILE & RUN

       Output:                                                               Output:

Iteration3

Input

COMPILE & RUN

Output:    

Example3:

try:

 

Input

COMPILE & RUN

Output:

Example4:

try:

 

           Iteration1                                                        Iteration2

           Input:                                                                    Input:

COMPILE & RUN

              Output:                                                      Output:  

Iteration3

Input

COMPILE & RUN

Output:      

PREV    NEXT


Share this website !!!
Facebook Twitter Linkedin