Created
February 3, 2015 11:05
-
-
Save jorge-lavin/e5736f141297625c8c6b to your computer and use it in GitHub Desktop.
Re raising exceptions sandbox
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
""" | |
In this example we try to re raise an exception changing its class | |
""" | |
import sys | |
class InputFileNotFoundError(IOError): | |
""" | |
Input file not found | |
""" | |
pass | |
def read_input_file(input_file): | |
""" | |
Reads the input file | |
@param input_file: The input file path | |
@type input_file: String | |
@return contents: The contents of the file in a string | |
@rtype contents: String | |
@raises InputFileNotFoundError | |
""" | |
try: | |
with open(input_file) as f: | |
return f.read() | |
except IOError: | |
exc_info = sys.exc_info() | |
raise InputFileNotFoundError, exc_info[1], exc_info[2] | |
if __name__ == '__main__': | |
input_file = 'this_does_not_exist.txt' | |
try: | |
read_input_file(input_file) | |
except IOError as e: | |
exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment