Managing exceptions in Python: Difference between revisions
No edit summary |
Mr. MacKenty (talk | contribs) |
||
(10 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
[[File:idea.png|right|frame|This is basic programming knowledge <ref>http://www.flaticon.com/</ref>]] | |||
==Introduction== | ==Introduction== | ||
Exception handling is the process of responding to the occurrence, during computation, of exceptions – anomalous or exceptional conditions requiring special processing – often changing the normal flow of program execution. It is provided by specialized programming language constructs or computer hardware mechanisms. | |||
In general, an exception is handled (resolved) by saving the current state of execution in a predefined place and switching the execution to a specific subroutine known as an exception handler. If exceptions are continuable, the handler may later resume the execution at the original location using the saved information. For example, a floating point divide by zero exception will typically, by default, allow the program to be resumed, while an out of memory condition might not be resolvable transparently. | |||
Alternative approaches to exception handling in software are error checking, which maintains normal program flow with later explicit checks for contingencies reported using special return values or some auxiliary global variable such as C's errno or floating point status flags; or input validation to preemptively filter exceptional cases. | |||
< | Some programmers write software with error reporting features that collect details that may be helpful in fixing the problem, and display those details on the screen, or store them to a file such as a core dump, or in some cases an automatic error reporting system such as Windows Error Reporting can automatically phone home and email those details to the programmers. <ref>https://en.wikipedia.org/wiki/Exception_handling</ref> | ||
==Example exception handling in Python== | |||
<syntaxhighlight lang="python" line="1" > | |||
try: | |||
number = input("Enter a number ") | |||
print("Your number was: " + str(number)) | |||
except NameError: | |||
print("Please use a number.") | |||
</syntaxhighlight> | |||
In the example above, if a user enters a string, for example 'abc', python would throw a NameError exception. This example was swiped from the official Python documentation which you should read. <ref>https://docs.python.org/2/tutorial/errors.html</ref> | |||
# | <syntaxhighlight lang="python" line="1" > | ||
# this code will generate a TypeError. We cannot concatenate a string and integer. | |||
my_age = 16 | |||
try: | |||
print("Your age is " + my_age) | |||
except TypeError: | |||
print("Hey. You should know better than to combine strings and integers!") | |||
</syntaxhighlight> | |||
In the example above, we have been silly and tried to concatenate a string and an integer! We are testing for a specific type of exception, TypeError. | |||
<syntaxhighlight lang="python" line="1" > | |||
my_age = 16 | |||
try: | |||
print("Your age is " + my_age) | |||
except Exception: | |||
print("Yikes. Something has gone horribly wrong.") | |||
</syntaxhighlight> | |||
In our final example, above, we are using a generic test for any possible exception. | |||
== List of possible exceptions == | |||
[http://www.tutorialspoint.com/python/python_exceptions.htm Please click here for a list of exceptions] | |||
==References== | ==References== | ||
<references /> | <references /> | ||
[[Category:This information is incomplete]] | |||
[[Category:Python]] | |||
[[Category:Exceptions]] | |||
[[Category:Programming Basics]] |
Latest revision as of 13:36, 18 May 2021
Introduction[edit]
Exception handling is the process of responding to the occurrence, during computation, of exceptions – anomalous or exceptional conditions requiring special processing – often changing the normal flow of program execution. It is provided by specialized programming language constructs or computer hardware mechanisms.
In general, an exception is handled (resolved) by saving the current state of execution in a predefined place and switching the execution to a specific subroutine known as an exception handler. If exceptions are continuable, the handler may later resume the execution at the original location using the saved information. For example, a floating point divide by zero exception will typically, by default, allow the program to be resumed, while an out of memory condition might not be resolvable transparently.
Alternative approaches to exception handling in software are error checking, which maintains normal program flow with later explicit checks for contingencies reported using special return values or some auxiliary global variable such as C's errno or floating point status flags; or input validation to preemptively filter exceptional cases.
Some programmers write software with error reporting features that collect details that may be helpful in fixing the problem, and display those details on the screen, or store them to a file such as a core dump, or in some cases an automatic error reporting system such as Windows Error Reporting can automatically phone home and email those details to the programmers. [2]
Example exception handling in Python[edit]
try:
number = input("Enter a number ")
print("Your number was: " + str(number))
except NameError:
print("Please use a number.")
In the example above, if a user enters a string, for example 'abc', python would throw a NameError exception. This example was swiped from the official Python documentation which you should read. [3]
# this code will generate a TypeError. We cannot concatenate a string and integer.
my_age = 16
try:
print("Your age is " + my_age)
except TypeError:
print("Hey. You should know better than to combine strings and integers!")
In the example above, we have been silly and tried to concatenate a string and an integer! We are testing for a specific type of exception, TypeError.
my_age = 16
try:
print("Your age is " + my_age)
except Exception:
print("Yikes. Something has gone horribly wrong.")
In our final example, above, we are using a generic test for any possible exception.
List of possible exceptions[edit]
Please click here for a list of exceptions