Course Content

If you are facing any issue in playing video please use Firefox browser.

Course Content

Description

Exception Handling in Python 1

 

In this section we will see that,

What is exception ?

What is error ?

Which are the different types of exception ?

Which are the terms to handle the exceptions in Python ?

 

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instruction.

 

Errors detected during execution are called exceptions.

 

In Python there are two distinguishable kinds of errors : syntax errors and exceptions.

 

Syntax errors, also known as parsing errors.

 

for i in range(1,10)

print(i)

Here missing":" after for is syntax error.

 

Exception

 

10*(1/0) : ZeroDivisionError : division by zero.

4+spam*3 : NameError : name 'spam' is not defined.

'2'+2 : TypeError : Can't convert 'int' object to str implicitly.

 

Example :

print('hey')

A=5/0

print(A)

print ('hey2')

O/P :

hey

**error**

 

Exception handling in Python

 

Eg. n=int(input("Please enter a number:"))

Please enter a number:23.50

Exception occurs like ValueError: invalid literal for int() with base 10: '23.5'

 

With all the the keywords mentioned below we can handle the exceptions of python -

try

except

else

finally

raise

 

try :

n=input("Please enter an integer:")

n=int(n)

except ValueError:

print("no valid integer! please try again")

print("great!! you successful entered an integer!")

 

A try statement may have more than one except clause.

But at most one except clause will be executed.

FAQs

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere