top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Python Try Except

Abhishek Tuladhar

While executing a python program, one may encounter several errors and exceptions. Errors are the problem that will cause the program execution to stop while exceptions are raised when some event changes the normal flow of the program. These exception errors can be: IOError, KeyboardInterrupt, ValueError, EOFError, etc.


But with the help of the Try and Except statement, these exception errors can be easily tackled without affecting the flow of the program.


Syntax:

try:
    # Some Code
except:
    # Executed if error in the try block

The try block is used to check some code for errors i.e. the code inside the try block will execute when there is no error in the program. Whereas the code inside the except block will execute whenever the program encounters some error in the preceding try block.


Example:

def divide(x, y):
    try:
        result = x // y
        print("Yeah ! Your answer is :", result)
    except ZeroDivisionError:
        print("Sorry ! You are dividing by zero ")


divide(3, 2)

divide(3, 0)


Output:


Yeah ! Your answer is : 1
Sorry ! You are dividing by zero 

In the above example, with the first function call divide(3, 2), the try() clause was executed as no exceptions were raised. However, for the second function call divide(3, 0), the except() clause was run as the exception was raised.



Else clause:

The try and except statement can be further extended by adding the Else clause, which is only executed if the try block does not raise an exception.


Syntax:

try:
    # Some Code
except:
    # Executed if error in the
    # try block
else:
    # execute if no exception

Example:

def divide(a , b):
    try:
        c = ((a+b) / (a-b))
    except ZeroDivisionError:
        print "Denominator is zero."
    else:
        print c
  
divide(2.0, 3.0)
divide(3.0, 3.0)

Output:

-5.0
Denominator is zero.


Finally

The finally block, if specified, will be executed regardless if the try block raises an error or not.

Syntax:

try:
    # Some Code
except:
    # Executed if error in the try block
else:
    # execute if no exception
finally:
    # Some code .....(always executed)

Example:

try:
    k = 5//0 
    print(k)
       
except ZeroDivisionError:   
    print("Can't divide by zero")
        
finally:
    print('This is always executed')


Output:

Can't divide by zero
This is always executed


0 comments

Recent Posts

See All

Comments


COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page