exception - Java, return new MyException: anti-pattern? -
in class i'm doing validation of custom data. many conditions apply. upon failure, want throw specific myexception. throwing myexception takes many common parameters, , 1 custom parameter (based upon actual failure). actual throw takes many characters write , destroys tidyness because of code duplication. have throw times. made mind create private method prepares , returns new instance of myexception , takes custom data parameter, code can cleaner.
private myexception createmyexception(final customerrordata errordata) { ... info gathering, parameterizing, etc... return new myexception(errordata); }
...
so throwing new myexception shorter:
throw createmyexception(errordata);
my question is: what's correct practice prevent code duplication in case? may overmistifying exceptions.
an exception factory - never seen before @ least sounds proper design.
i worry - seem put quite lot effort on designing exception throwing framework: adding parameters, states, etc. exceptions. encounter that many exceptional conditions in code? or throw exceptions proper handling of expected conditions would?
usually thrown exception "just logs". happened shouldn't have happened in current context. something, developers should know , correct in next release. shouldn't use exceptions handle expected states.
so before investigating in brilliant exception creation code, double-check if it's worth effort or if design of application starting ... creative.
Comments
Post a Comment