bit of coding newbie here , looking advice!
i have following method - simple weight converter kilograms pounds, that's part of larger console application. user selects conversion scheme want , enter weight , convert it. doing unit tests better understanding of them , decided add code return exception if user enters minus number, make more robust. code follows:
public static double kilogramstopounds(string weightinkilos) { //convert parameter double calculation double kiloweight = double.parse(weightinkilos); //convert kilograms pounds double poundweight = kiloweight * 2.20462; try { if (kiloweight < 0) { throw new argumentoutofrangeexception(); } else { return poundweight; } } catch (argumentoutofrangeexception argex) { console.writeline(argex); } return 0; }
however, when runs return 0 because requires double return type. argumentoutofrange exception, , 0 because requires double.
i wondering if there way "if enter number below 0, error message, if enter valid positive number, correct numerical result"? because seems no matter need provide numerical value happy error message, or else "not code paths return value" error.
any appreciated.
note - should add string parameter "weightinkilos" taken user's console input. code show choices in different file, , conversion rates in separate file.
remove try-catch
method. throw exception. method not supposed catch exceptions throws itself.
put try-catch
around call kilogramstopounds
.
Comments
Post a Comment