c# - Error handling of user input in a List<int> -
i have following code:
list<int> moneys = new list<int>(); console.writeline("please enter cost of choice"); int money = int.parse(console.readline()); moneys.add(money);
from if enter text program stops working , unhandled exception message appears. wondering how handle exception, if possible program doesn't stop working?
you should use tryparse method. not throw exception if input not valid. this
int money; if(int.tryparse(console.readline(), out money)) moneys.add(money);
Comments
Post a Comment