move IOError handling to a function

Don't repeat yourself :)

Signed-off-by: Michael Walle <michael.walle@kontron.com>