Next ❯ ❮ Previous

Introduction to Python Exception

python execption

Let’s see some examples of exceptions;

>>> 10/0
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
10/0
ZeroDivisionError : division by zero
>>> 'silan'+10
Traceback (most recent call last):
File"<pyshell#1>", line 1, in <module>
'silan'+10
TypeError: can only concatenate str (not "int") to str

When the exception arises, program terminates execution. This is called crash.So we have to handle exception.

Let’s see one program how to handle exception:

python execption
Explanation:

Here in the above source code the exception handling mechanism consisting try and except keyword. Basically trycontains risky code, that means on which statement, an exception raise that statement we will write in try block. Once a tryblock executes, then control come to the except part and verify the type of exception raised in try block and type of exception reference have taken or not in except part. It should be matched. Then exceptpart executes. Basically except part contain handling code.


So here in try block the exception ZeroDivisionError raised and in except part ZeroDivisionError reference that we have taken. So except part executed , as a result remaining statement executed and finally we got the output. Or we can say program terminated normally.


Next ❯ ❮ Previous

Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved