Mechanism of Exception in Python
Exception !!! the term when occurs in code, may crash your code, make your program quit undesirably, forcefully. This exception arises due to the program written like DivideByZeroException, IOError etc. Each computer language has its own way to tackle the Exceptions. Python is no longer an exception in this regards. It has a beautiful Try-Except mechanism to deal with run time errors in the code.Following is the syntax
try:#1 code goes hereexcept:#code goes here to handle the situation#in case you may want some operations happen ,use Finally blockfinally:#code goes here to perform operations like memory handling, file closing etc
- This refers to single line comment in Python [back]
- Dont FORGET to put : at the end of try-except and finally statements or interpreter gives you error
Remember when an error or exception occurs , there comes track-stack as output. It states the type of error,along with certain other information about the error. Also it defines the line where exception occurs .
Also do try to use try-except block in your code always ,in order to provide security to code .Also some time it happens the after closing the file in python,it crashes ,thereby the data may lost.To protect code from such situation ,it is Strongly recommended to use try-except block always.Have a happy coding