Skip to content

Instantly share code, notes, and snippets.

@jorge-lavin
Created February 3, 2015 11:05
Show Gist options
  • Save jorge-lavin/e5736f141297625c8c6b to your computer and use it in GitHub Desktop.
Save jorge-lavin/e5736f141297625c8c6b to your computer and use it in GitHub Desktop.
Re raising exceptions sandbox
"""
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