Sunday, 15 September 2013

Java Programming , returning in case of an error -



Java Programming , returning in case of an error -

i want know the best programming practice in next use-case method mymethod--

1) mymethod has numerical purpose - modify contents of array private variable of class

2) before so, need run critical checks on numbers,say check1, check2, check3, of if fail, there no point in going ahead. eg. check might check negative numbers in array.

so brings question, should mymethod return, how should calling function told checkx has failed.

you can create custom checked exception follows:

class arraymodificationexception extends exception{ public arraymodificationexception(string message){ super(message); } }

now in "mymethod" add together following:

void mymethod() throws arraymodificationexception{ //code check conditions before modifications //code modify array if(check fails){ throw new arraymodificationexception("cusom message"); } }

where custom message specific message conveying exact reason of failure.

of course of study called decide if handle or re-throw it. if 1 of conditions code should not seek recover can design run-time exception , throw without throws clause method

java

No comments:

Post a Comment