FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
2.8k views
in Python Programming by Goeduhub's Expert (3.1k points)
edited by

Exception handling is an important concept in python to run your code smoothly without interruption of errors. In this article we will see how the try-except block works to avoid exceptions/errors and run code smoothly. We will also see how to raise an error/exception and how to handle it.

Related Links: Basic Python 

by (100 points)
Good material in this module.

3 Answers

2 like 0 dislike
by Goeduhub's Expert (3.1k points)
edited by
 
Best answer

Python Exception/Error handling 

  • In python when we execute a program sometimes python gives error, here we learn how to handle these type of errors in python.
  • exception is an error basically which occurs during the time of execution, when program disrupts the normal flow of the program's instructions.
  • Exception is a base class for all exceptions in python.

Python provides several types of exceptions, below given some examples of  exception to understand  

Exceptions / error Description
Syntax error When code syntax is not proper 
Type error Raised  when an operation or function is attempted that is invalid for the specified data type.
Zero   Division ErrorWhen you tried  to divide by zero
IOErrorOperating system related error 
IndexErrorWhen index is not found or out of range
Indentation error Raised when indentation is not specified properly.
Memory error When program runs out of memory 
  • In python we use try- except  to handle the exception. 
  • The try block tests the statement of error.
  • The except block handle the error.
  • 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.

Exception Blocks and how they perform :

try:

#Run  first

(Program / statements)

except:

#Run if exception occur in try block

(Program  /statement)

else:

#Run if try block succeeds 

(Program/statement)

finally:

#Always execute

(program/ statement)

  

If-except blocks

Examples (how to handle index error)

array = [1, 2, 3,4,5,6] 

try:  

    print ("third element = ", (array[2]) )

   # Throws error since there are only 6 elements in array 

    print ("Eight element =" (array[7]))  

except IndexError: 

    print ("An error occurred")

Output 

third element = 3 An error occurred

try-except-else blocks

Examples (Zero division error ) 

 try:  

    a = int(input("Enter a:"))  

    b = int(input("Enter b:"))  

    c = a/b;  

    print(c)  

except ZeroDivisionError:  

    print("can't divide by zero")  

else:  

       print("Nothing wrong ")   

Output

Enter a:4 Enter b:0 

can't divide by zero 

Raising exceptions

An exception can be raised by using the raise clause in python.This statement allows the programmer to force a specific exception to occur. 

  • In raise statement we need to define exception class.

Example

try:  

    age = int(input("Enter the age?"))  

    if age<18:  

        raise ValueError;  

    else:  

        print("the age is valid")  

except ValueError:  

     print("The age is not valid")  

Output

Enter the age?7 

The age is not valid 

Note:-If you want to check your raised error you have to just type raise at the end of the code and you will see the error caused by raise  error statement.

try-finally

This clause is executed no matter what, and is generally used to release external resources.(clean up the resources once used)

try:

    f=open("Data.txt")

   # perform file operations

finally:

 f.close()

Note:-The finally clause is optional. It is intended to define clean-up actions type.


Python Tutorial 

Machine Learning Tutorial 

AI Tutorial

Free Online Tutorials

by (101 points)
i read it  completely
by (412 points)
this is the improper page of python tkinter module.
by (294 points)
In this chapter I learned that how to handle error when the error is occure during the execution of program
by (294 points)
In this chapter I learned that how to handle error when the error is occure during the execution of programduring the flow of program
by (100 points)
Ma'am please explain the raise exception with a real life example as i did not understand its proper use.
by (422 points)
In this above article we get to know how to handle error in python.
by (100 points)
In this article, i learned about exception which is basically an error that occurs during the time of execution. we can handle exception using try except in python. there are different types of exceptions like ValueError, IndexError etc.
0 like 0 dislike
by (391 points)

Date: 02/05/2020

Topic: Python Exception Handling

here, i have learned what kind of errors we can face during codding and what that mean and also learned how we can handle it.

0 like 0 dislike
by (294 points)
in this module i hava learned the exeption and the error is occure during the execution of the program

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

0 like 0 dislike
5 answers 2.5k views
0 like 0 dislike
1 answer 391 views
asked Sep 18, 2020 in Python Programming by Nisha Goeduhub's Expert (3.1k points)
0 like 0 dislike
1 answer 2.0k views
0 like 0 dislike
6 answers 3.2k views
asked Mar 16, 2020 in Python Programming by Nisha Goeduhub's Expert (3.1k points)
1 like 0 dislike
3 answers 2.8k views

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...