Exceptions<\/strong><\/p>\nThe exception is a way of breaking out of the normal flow of control of a code block in order to handle the error or other exceptional conditions. An exception is raised at the point where the error is detected.<\/p>\n
>>> while True print ' Hello world '\r\nSyntaxError: invalid syntax \r\n>>> 10 \/ 0\r\n\r\nTraceback (most recent call last) : \r\nFile \"<pyshell#l>\", line 1, in <module>\r\n\u00a0 \u00a0 10 \/ 0 \r\nZeroDivisionError: integer division or modulo by zero \r\n>>> 4+tt*3\r\n\r\nTraceback (most recent call last) :\r\nFile \"<pyshe11#2>\", line 1, in <module>\r\n4+tt*3\r\nNameError: name ' tt ' is not defined \r\n>>> ' 5 '+7\r\n\r\nTraceback (most recent call last) :\r\nFile \"<pyshe11#3>\", line 1, in <module>\r\n' 5 '+7\r\nTypeError: cannot concatenate ' str ' and ' int ' objects<\/pre>\nThe last line of the error message indicates what went wrong. Exceptions are of different types, and the type is printed as part of the message; the types in the above example are SyntaxError, ZeroDivisionError, NameError, and TypeError. Standard exception names are built-in identifiers (not reserved keywords). The rest of the lines provides detail based on the type of exception and what caused it.<\/p>\n
Handling exceptions<\/strong><\/p>\nIf there is some suspicious code that may raise an exception, it can be handled by placing the suspicious code in a try compound statement. After the try clause, include an except clause, followed by a block of code that handles the problem. The following example attempts to open a file and write something in the file.<\/p>\n
# ! \/ usr \/ bin \/ python \r\ntry :\r\n\u00a0 \u00a0 \u00a0fh = open ( \" testflie \" , \" w \" )\r\n\u00a0 \u00a0 \u00a0fh.write ( \" This is my test file for exception handling ! ! \" ) \r\nexcept IOError :\r\nprint \" Error: can\\'t find the file or read data \" \r\nelse: \r\nprint \" Written content in the file successfully \" \r\nfh.close ( )<\/pre>\nHere are few important points that need to be remembered:<\/p>\n
\n- A single try statement can have multiple except clauses. This is useful when the try clause contains statements that may throw different types of exceptions.<\/li>\n
- A generic except clause can be provided, which handles any exception.<\/li>\n
- After the except clause(s), and else clause can be included. The code in the else clause is executed, if the code in the try clause does not raise an exception.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"
In this Page, We are Providing Python Programming \u2013 Exceptions. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Exceptions Exceptions The exception is a way of breaking out of the normal flow of control of a code block in order to handle the error or other exceptional conditions. …<\/p>\n
Python Programming \u2013 Exceptions<\/span> Read More »<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Programming \u2013 Exceptions - Python Programs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n