Saturday 15 February 2014

parsing - Java exception handling in parsers -



parsing - Java exception handling in parsers -

let's have such simple parser:

public class resourcemanager { private final static logger log = logger.getlogger(resourcemanager.class); public museum parsejsonfile(string filepath) /*throws ioexception ???? */ { museum museum = null; seek { objectmapper objectmapper = new objectmapper(); museum = objectmapper.readvalue(new file(filepath), museum.class); } catch(ioexception e) { log.error(e); } homecoming museum; } }

should exception caught in method or in calling code? variant better?

parser can't exception, that's exceptional situation parser , can't produce expectable result. outside should handle it.

in particular, should not homecoming null result in bunch of null-checks in calling code (which can forget put, or due lack of documentation on implementation don't know whether have check null without seeing code). 1 of problems exceptions in java intended solve. declaring checked-exception in method signature, you're enforcing users of parser deal fact value might not come.

java parsing exception-handling

No comments:

Post a Comment