Python programming- finally


Using finally in error handling


The statements in finally block still execute although some errors are raised or not. The common use of finally block is to clear objects from memory.
>>> def sumrange(lst):
            result=0
            for i in lst:
                        result+=i
            return result
>>> lst=range(1,10)
>>> try:
            sumrange(lst)
except Excepton:print 'Error(s) in sum operation'
finally:
            del lst
            print 'The object has been cleared.'
           
45
The object has been cleared.
>>> 
 
Note: You can use Exception class to catch any errors that you are not sure about.


Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.