{"id":24,"date":"2023-02-16T06:07:00","date_gmt":"2023-02-16T06:07:00","guid":{"rendered":""},"modified":"2024-01-28T09:05:29","modified_gmt":"2024-01-28T09:05:29","slug":"python-exceptions-handling","status":"publish","type":"post","link":"https:\/\/pythonpower.in\/index.php\/2023\/02\/16\/python-exceptions-handling\/","title":{"rendered":"PYTHON EXCEPTIONS HANDLING"},"content":{"rendered":"<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"http:\/\/pythonpower.in\/wp-content\/uploads\/2023\/02\/python-exception-handling.png\" style=\"margin-left: 1em; margin-right: 1em;\"><img loading=\"lazy\" decoding=\"async\" border=\"0\" data-original-height=\"426\" data-original-width=\"757\" height=\"360\" src=\"http:\/\/pythonpower.in\/wp-content\/uploads\/2023\/02\/python-exception-handling-300x169.png\" width=\"640\" \/><\/a><\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: large;\"><b>PYTHON EXCEPTIONS HANDLING<\/b><\/span><\/p>\n<p>Python provides two very important features to handle any unexpected error in your Python<br \/>\nprograms and to add debugging capabilities in them \u2212<\/p>\n<p><span style=\"font-size: medium;\"><b>. Exception Handling:&nbsp;<\/b><\/span>This would be covered in this tutorial. Here is a list standard<br \/>\nExceptions available in Python: Standard Exceptions.<\/p>\n<p>.<span style=\"font-size: medium;\"><b> Assertions:&nbsp;<\/b><\/span>This would be covered in Assertions in Python tutorial.<\/p>\n<p>List of Standard Exceptions \u2212<\/p>\n<p><span style=\"font-size: medium;\"><b>EXCEPTION NAME&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/b><\/span><b style=\"font-size: large;\">&nbsp;<\/b><\/p>\n<p><b>Exception&nbsp;<\/b>&nbsp;<\/p>\n<p>Base class for all exceptions&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n<p><b>StopIteration<\/b><\/p>\n<p>Raised when the next method of an iterator does not point to any object.&nbsp;<b>&nbsp; <\/b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/p>\n<p><b>SystemExit<\/b><\/p>\n<p>Raised by the sys.exit function.&nbsp;<b>&nbsp; <\/b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n<p><b>StandardError&nbsp;<\/b><\/p>\n<p>Base class for all built-in exceptions except StopIteration and SystemExit.&nbsp;<b>&nbsp;&nbsp; <\/b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/p>\n<p><b>ArithmeticError&nbsp;<\/b><\/p>\n<p><b><\/b>&nbsp;Base class for all errors that occur for numeric&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; calculation.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/p>\n<p><b>OverflowError<\/b><\/p>\n<p>Raised when a calculation exceeds maximum limit&nbsp; for a numeric type.&nbsp;<\/p>\n<p><b>FloatingPointError<\/b><\/p>\n<p>Raised when a floating point calculation fails.&nbsp;<b>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/b><\/p>\n<p><b>&nbsp; &nbsp;<\/b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n<p><b>ZeroDivisonError&nbsp;<\/b><\/p>\n<p>&nbsp;Raised when division or modulo by zero takes&nbsp; place for all numeric types.&nbsp;<\/p>\n<p><b>AssertionError&nbsp;<\/b><\/p>\n<p>Raised in case of failure of the Assert statement.&nbsp;<b>&nbsp;&nbsp; &nbsp;<\/b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n<p><b>&nbsp;&nbsp;<\/b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n<p><b>AttributeError&nbsp;<\/b><\/p>\n<p>Raised in case of failure of attribute reference or&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; assignment.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n<p><b>EOFError&nbsp;<\/b><\/p>\n<p>Raised when there is no input from either the&nbsp; raw_input or input function and the end of file is&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reached.<b>&nbsp;&nbsp; &nbsp;<\/b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n<p><b>ImportError&nbsp;<\/b><\/p>\n<p>&nbsp;Raised when an import statement fails.<b>&nbsp;&nbsp;<\/b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/p>\n<p><b>KeyboardInterrupt&nbsp;<\/b><\/p>\n<p>Raised when the user interrupts program execution, usually by pressing Ctrl+c.<b>&nbsp;&nbsp; &nbsp; <\/b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/p>\n<p><b>LookupError<\/b>&nbsp;&nbsp;<\/p>\n<p>Base class for all lookup errors.&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/p>\n<p><b>IndexError<br \/>\nKeyError&nbsp;<\/b><\/p>\n<p>Raised when an index is not found in a sequence.Raised when the specified key is not found in dictionary.<\/p>\n<p><b>NameError&nbsp;&nbsp;<\/b><\/p>\n<p>Raised when an identifier is not found in the local or&nbsp; &nbsp;global namespace<b>&nbsp;&nbsp;<\/b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n<p><b>UnboundLocalError<\/b><\/p>\n<p>Raised when trying to access a local variable in a function or method but no value has been assigned&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;to it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n<p><b>&nbsp;IOError&nbsp;<\/b><\/p>\n<p>Raised when an input\/ output operation fails, such&nbsp; &nbsp;as the print statement or the open function when&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trying to open a file that does not exist.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<b>&nbsp;&nbsp; <\/b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n<p><b>IOError&nbsp;<\/b>&nbsp;<\/p>\n<p>Raised for operating system-related errors.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/p>\n<p><b>SyntaxError&nbsp; &nbsp;<\/b><\/p>\n<p>Raised when there is an error in Python syntax.&nbsp;<\/p>\n<p><b>IndentationError&nbsp; <\/b>&nbsp;<\/p>\n<p>Raised when indentation is not specified properly.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n<p><b>SystemError&nbsp;<\/b>&nbsp;<\/p>\n<p>Raised when the interpreter finds an internal&nbsp; &nbsp;problem, but when this error is encountered the&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Python interpreter does not exit.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n<p><b>SystemExit&nbsp;<\/b>&nbsp;<\/p>\n<p>Raised when Python interpreter is quit by using the&nbsp; sys.exit function. If not handled in the code, causes the interpreter to exit.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n<p><b>ValueError&nbsp;<\/b><\/p>\n<p>Raised when the built-in function for a data type has the&nbsp; valid type of arguments, but the arguments have&nbsp; invalid values specified.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/p>\n<p><b><\/b>&nbsp;<b>RuntimeError&nbsp;<\/b><\/p>\n<p><b><\/b>Raised when a generated error does not fall into any&nbsp; category.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n<p><b>NotImplementedError&nbsp;<\/b><\/p>\n<p>Raised when an abstract method that needs to be&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; implemented in an inherited class is not actually implemented.<\/p>\n<p><\/p>\n<p><b><\/b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n<p><span style=\"font-size: medium;\"><b>Assertions in Python<\/b><\/span><\/p>\n<p>An assertion is a sanity-check that you can turn on or turn off when you are done with your testing<br \/>\nof the program.&nbsp;<\/p>\n<p>The easiest way to think of an assertion is to liken it to a raise-if statement<br \/>\nortobemoreaccurate, araise \u2212 if \u2212 notstatement. An expression is tested, and if the result comes up false, an<br \/>\nexception is raised.&nbsp;<\/p>\n<p>Assertions are carried out by the assert statement, the newest keyword to Python, introduced in<br \/>\nversion 1.5.<br \/>\nProgrammers often place assertions at the start of a function to check for valid input, and after a<br \/>\nfunction call to check for valid output.<\/p>\n<p><span style=\"font-size: medium;\"><b>The assert Statement<\/b><\/span><\/p>\n<p>When it encounters an assert statement, Python evaluates the accompanying expression, which is<br \/>\nhopefully true. If the expression is false, Python raises an AssertionError exception.<\/p>\n<p>The syntax for assert is \u2212<\/p>\n<p>assert Expression[, Arguments]<\/p>\n<p>If the assertion fails, Python uses ArgumentExpression as the argument for the AssertionError.<br \/>\nAssertionError exceptions can be caught and handled like any other exception using the try-except<br \/>\nstatement, but if not handled, they will terminate the program and produce a traceback.<\/p>\n<p><span style=\"font-size: large;\"><b>Example<\/b><\/span><\/p>\n<p>Here is a function that converts a temperature from degrees Kelvin to degrees Fahrenheit. Since<br \/>\nzero degrees Kelvin is as cold as it gets, the function bails out if it sees a negative temperature \u2212<\/p>\n<p>#!\/usr\/bin\/python&nbsp;<\/p>\n<p>def KelvinToFahrenheit(Temperature):&nbsp;<\/p>\n<p>assert (Temperature &gt;= 0),&#8221;Colder than absolute zero!&#8221;&nbsp;<\/p>\n<p>return ((Temperature-273)*1.8)+32&nbsp;<\/p>\n<p>print KelvinToFahrenheit(273)&nbsp;<\/p>\n<p>print int(KelvinToFahrenheit(505.78))&nbsp;<\/p>\n<p>print KelvinToFahrenheit(-5)<\/p>\n<p>When the above code is executed, it produces the following result \u2212<\/p>\n<p>32.0&nbsp;<\/p>\n<p>451&nbsp;<\/p>\n<p>Traceback (most recent call last):&nbsp;<\/p>\n<p>File &#8220;test.py&#8221;, line 9, in<\/p>\n<p>&nbsp;print KelvinToFahrenheit(-5)&nbsp;<\/p>\n<p>File &#8220;test.py&#8221;, line 4, in KelvinToFahrenheit&nbsp;<\/p>\n<p>assert (Temperature &gt;= 0),&#8221;Colder than absolute zero!&#8221;&nbsp;<\/p>\n<p>AssertionError: Colder than absolute zero!<\/p>\n<p><span style=\"font-size: medium;\"><b>What is Exception?<\/b><\/span><\/p>\n<p>An exception is an event, which occurs during the execution of a program that disrupts the normal<br \/>\nflow of the program&#8217;s instructions. In general, when a Python script encounters a situation that it<br \/>\ncannot cope with, it raises an exception. An exception is a Python object that represents an error.<\/p>\n<p>When a Python script raises an exception, it must either handle the exception immediately<br \/>\notherwise it terminates and quits.<\/p>\n<p><span style=\"font-size: medium;\"><b>Handling an exception<\/b><\/span><\/p>\n<p>If you have some suspicious code that may raise an exception, you can defend your program by<br \/>\nplacing the suspicious code in a<b> try:<\/b> block. After the try: block, include an <b>except:<\/b> statement,<br \/>\nfollowed by a block of code which handles the problem as elegantly as possible.<\/p>\n<p><span style=\"font-size: medium;\"><b>Syntax<\/b><\/span><\/p>\n<p>Here is simple syntax of try&#8230;.except&#8230;else blocks \u2212<\/p>\n<p>try:<br \/>\nYou do your operations here;&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.&nbsp;<\/p>\n<p>except ExceptionI:&nbsp;<\/p>\n<p>If there is ExceptionI, then execute this block.&nbsp;<\/p>\n<p>except ExceptionII:&nbsp;<\/p>\n<p>If there is ExceptionII, then execute this block.<\/p>\n<p>&nbsp; &nbsp; &nbsp;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.&nbsp;<\/p>\n<p>else:&nbsp;<\/p>\n<p>If there is no exception then execute this block.<\/p>\n<p>Here are few important points about the above-mentioned syntax \u2212<\/p>\n<p><span style=\"font-size: large;\"><b>.&nbsp;<\/b><\/span>A single try statement can have multiple except statements. This is useful when the try block<br \/>\ncontains statements that may throw different types of exceptions.<\/p>\n<p><b style=\"font-size: x-large;\">.&nbsp;<\/b>You can also provide a generic except clause, which handles any exception.<\/p>\n<p><span style=\"font-size: medium;\"><b>.<\/b><\/span> After the except clauses, you can include an else-clause. The code in the else-block executes<br \/>\nif the code&nbsp; &nbsp; &nbsp; &nbsp;in the try: block does not raise an exception.<\/p>\n<p><span style=\"font-size: large;\"><b>. <\/b><\/span>The else-block is a good place for code that does not need the try: block&#8217;s protection.<\/p>\n<p><span style=\"font-size: large;\"><b>Example<\/b><\/span><\/p>\n<p>This example opens a file, writes content in the, file and comes out gracefully because there is no<br \/>\nproblem at all \u2212<\/p>\n<p>#!\/usr\/bin\/python<\/p>\n<p>&nbsp;try:&nbsp;<\/p>\n<p>fh = open(&#8220;testfile&#8221;, &#8220;w&#8221;)&nbsp;<\/p>\n<p>fh.write(&#8220;This is my test file for exception handling!!&#8221;)<\/p>\n<p>&nbsp;except IOError:&nbsp;<\/p>\n<p>print &#8220;Error: can&#8217;t find file or read data&#8221;&nbsp;<\/p>\n<p>else:&nbsp;<\/p>\n<p>print &#8220;Written content in the file successfully&#8221;<br \/>\nfh.close()<\/p>\n<p>This produces the following result \u2212<\/p>\n<p><b>Written content in the file successfully<\/b><\/p>\n<p><span style=\"font-size: large;\"><b>Example<\/b><\/span><\/p>\n<p>This example tries to open a file where you do not have write permission, so it raises an exception-<\/p>\n<p>#!\/usr\/bin\/python&nbsp;<\/p>\n<p>try:&nbsp;<\/p>\n<p>fh = open(&#8220;testfile&#8221;, &#8220;r&#8221;)&nbsp;<\/p>\n<p>fh.write(&#8220;This is my test file for exception handling!!&#8221;)&nbsp;<\/p>\n<p>except IOError:&nbsp;<\/p>\n<p>print &#8220;Error: can&#8217;t find file or read data&#8221;&nbsp;<\/p>\n<p>else:&nbsp;<\/p>\n<p>print &#8220;Written content in the file successfully&#8221;<\/p>\n<p>This produces the following result \u2212<\/p>\n<p><b>Error: can&#8217;t find file or read data<\/b><\/p>\n<p><b><br \/><\/b><\/p>\n<p><span style=\"font-size: medium;\"><b>The except Clause with No Exceptions<\/b><\/span><\/p>\n<p>You can also use the except statement with no exceptions defined as follows \u2212<\/p>\n<p>try:&nbsp;<\/p>\n<p>You do your operations here;&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.&nbsp;<\/p>\n<p>except:&nbsp;<\/p>\n<p>If there is any exception, then execute this block.<\/p>\n<p>&nbsp;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.&nbsp;<\/p>\n<p>else:&nbsp;<\/p>\n<p>If there is no exception then execute this block.<\/p>\n<p>This kind of a <b>try-except<\/b> statement catches all the exceptions that occur. Using this kind of tryexcept statement is not considered a good programming practice though, because it catches all<br \/>\nexceptions but does not make the programmer identify the root cause of the problem that may<br \/>\noccur.<\/p>\n<p><span style=\"font-size: medium;\"><b>The except Clause with Multiple Exceptions<\/b><\/span><\/p>\n<p>You can also use the same except statement to handle multiple exceptions as follows<\/p>\n<p>try:&nbsp;<\/p>\n<p>&nbsp; &nbsp;You do your operations here;&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.&nbsp;<\/p>\n<p>except(Exception1[, Exception2[,&#8230;ExceptionN]]]):&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; If there is any exception from the given exception list,<br \/>\nthen execute this block.&nbsp;<\/p>\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.&nbsp;<\/p>\n<p>else:&nbsp;<\/p>\n<p>&nbsp; &nbsp; If there is no exception then execute this block.<\/p>\n<p><span style=\"font-size: medium;\"><b>The try-finally Clause<\/b><\/span><\/p>\n<p>You can use a <b>finally:<\/b> block along with a <b>try: <\/b>block. The finally block is a place to put any code<br \/>\nthat must execute, whether the try-block raised an exception or not. The syntax of the try-finally<br \/>\nstatement is this \u2212<\/p>\n<p>try:&nbsp;<\/p>\n<p>You do your operations here;<\/p>\n<p>&nbsp;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.&nbsp;<\/p>\n<p>Due to any exception, this may be skipped.&nbsp;<\/p>\n<p>finally:&nbsp;<\/p>\n<p>This would always be executed.<\/p>\n<p>Note that you can provide except clauses, or a finally clause, but not both. You cannot use else<br \/>\nclause as well along with a finally clause.<\/p>\n<p><span style=\"font-size: large;\"><b>Argument of an Exception<\/b><\/span><\/p>\n<p>An exception can have an argument, which is a value that gives additional information about the<br \/>\nproblem. The contents of the argument vary by exception. You capture an exception&#8217;s argument<br \/>\nby supplying a variable in the except clause as follows \u2212<\/p>\n<p>try:&nbsp;<\/p>\n<p>You do your operations here;&nbsp;<\/p>\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.&nbsp;<\/p>\n<p>except ExceptionType, Argument:&nbsp;<\/p>\n<p>You can print value of Argument here&#8230;<\/p>\n<p>If you write the code to handle a single exception, you can have a variable follow the name of the<br \/>\nexception in the except statement. If you are trapping multiple exceptions, you can have a<br \/>\nvariable follow the tuple of the exception.<\/p>\n<p>This variable receives the value of the exception mostly containing the cause of the exception. The<br \/>\nvariable can receive a single value or multiple values in the form of a tuple. This tuple usually<br \/>\ncontains the error string, the error number, and an error location.<\/p>\n<p><span style=\"font-size: medium;\"><b>Example<\/b><\/span><\/p>\n<p>Following is an example for a single exception \u2212<\/p>\n<p>#!\/usr\/bin\/python&nbsp;<\/p>\n<p># Define a function here.&nbsp;<\/p>\n<p>def temp_convert(var):&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp;try:&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; return int(var)&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;except ValueError, Argument:&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print &#8220;The argument does not contain numbersn&#8221;,&nbsp; Argument&nbsp;<\/p>\n<p># Call above function here.&nbsp;<\/p>\n<p>temp_convert(&#8220;xyz&#8221;);<\/p>\n<p>This produces the following result \u2212<\/p>\n<p><b>The argument does not contain numbers&nbsp;<\/b><\/p>\n<p><b>invalid literal for int() with base 10: &#8216;xyz&#8217;<\/b><\/p>\n<p><span style=\"font-size: medium;\"><b>Raising an Exceptions<\/b><\/span><\/p>\n<p>You can raise exceptions in several ways by using the <b>raise<\/b> statement. The general syntax for the<br \/>\nraise statement is as follows.<\/p>\n<p><span style=\"font-size: medium;\"><b>Syntax<\/b><\/span><\/p>\n<p>raise [Exception [, args [, traceback]]<\/p>\n<p>Here, Exception is the type of exception forexample, NameError and argument is a value for the<br \/>\nexception argument. The argument is optional; if not supplied, the exception argument is None<\/p>\n<p>The final argument, traceback, is also optional andrarelyusedinpractice, and if present, is the traceback<br \/>\nobject used for the exception.<\/p>\n<p><span style=\"font-size: medium;\"><b>Example<\/b><\/span><\/p>\n<p>An exception can be a string, a class or an object. Most of the exceptions that the Python core<br \/>\nraises are classes, with an argument that is an instance of the class. Defining new exceptions is<br \/>\nquite easy and can be done as follows \u2212<\/p>\n<p>def functionName( level ):<\/p>\n<p>if level &lt; 1:&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;raise &#8220;Invalid level!&#8221;, level&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# The code below to this would not be executed&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # if we raise the exception<\/p>\n<p><span style=\"font-size: medium;\"><b>Note: <\/b><\/span>In order to catch an exception, an &#8220;except&#8221; clause must refer to the same exception thrown<br \/>\neither class object or simple string. For example, to capture above exception, we must write the<br \/>\nexcept clause as follows \u2212<\/p>\n<p>try:&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp; Business Logic here&#8230;&nbsp;<\/p>\n<p>except &#8220;Invalid level!&#8221;:&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp; Exception handling here&#8230;&nbsp;<\/p>\n<p>else:&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp; Rest of the code here&#8230;<\/p>\n<p><span style=\"font-size: large;\"><b>User-Defined Exceptions<\/b><\/span><\/p>\n<p>Python also allows you to create your own exceptions by deriving classes from the standard built-in<br \/>\nexceptions.<\/p>\n<p>Here is an example related to RuntimeError. Here, a class is created that is subclassed from<br \/>\nRuntimeError. This is useful when you need to display more specific information when an<br \/>\nexception is caught.<\/p>\n<p>In the try block, the user-defined exception is raised and caught in the except block. The variable e<br \/>\nis used to create an instance of the class Networkerror.<\/p>\n<p>class Networkerror(RuntimeError):&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; def __init__(self, arg):&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.args = arg<\/p>\n<p><\/p>\n<p>So once you defined above class, you can raise the exception as follows \u2212<\/p>\n<p>try:&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp;raise Networkerror(&#8220;Bad hostname&#8221;)&nbsp;<\/p>\n<p>except Networkerror,e:&nbsp;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print e.args&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; PYTHON EXCEPTIONS HANDLING Python provides two very important features to handle any unexpected error&hellip;<\/p>\n","protected":false},"author":2,"featured_media":91,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":"","_joinchat":[]},"categories":[6],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/posts\/24"}],"collection":[{"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/comments?post=24"}],"version-history":[{"count":1,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":92,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/posts\/24\/revisions\/92"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/media\/91"}],"wp:attachment":[{"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/media?parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/categories?post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/tags?post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}