Make Exceptions work [closed]

There are some flaws in your code:

  • you should prefer int.TryParse instead of Parse. An Exception should be an unexpected behavior, where you have to react in some way. A userinput is not unexpected, you know there’s a chance that the use inputs invalid data which you can/have to validate.

  • When you use exceptions, you should not catch all exceptions at once, but some type of exception where you know how to react. int.Parse itself throws three kind of exceptions (see http://msdn.microsoft.com/de-de/library/b3h1hf19(v=vs.110).aspx), you can get some other ones from the system itself. Your code should catch FormatException instead of a catch-all.

  • anyway, if you just want to fix your code, you can solve your problem with using two seperate try .. catch blocks with seperated error messages.

Browse More Popular Posts

Leave a Comment