used exception to catch writing to file error

Signed-off-by: Nicholas Lum <nicholaslum444@hotmail.com>