Created
February 7, 2013 21:26
-
-
Save lambdamusic/4734368 to your computer and use it in GitHub Desktop.
Python: Python: error handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Error handling in Python is done through the use of exceptions that are caught in try blocks and handled in except blocks. If an error is encountered, a TRy block code execution is stopped and transferred down to the except block, as shown in the following syntax: | |
try: | |
f = open("test.txt") | |
except IOError: | |
print "Cannot open file." | |
## In addition to using an except block after the try block, you can also use the finally block. The code in the finally block will be executed regardless of whether an exception occurs. | |
f = open("test.txt") | |
try: | |
f.write(data) | |
. . . | |
finally: | |
f.close() | |
## You can raise an exception in your own program by using the raise exception [, value] statement. The value of exception is one of the built-in Python exceptions or a custom-defined exception object. The value of value is a Python object that you create to give details about the exception. Raising an exception breaks current code execution and returns the exception back until it is handled. The following example shows how to raise a generic RuntimeError exception with a simple text message value: | |
raise RuntimeError, "Error running script" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment