S. Justin Gengo wrote:
Alexey,
You should do some research on using Try/Catch to take care of
programming logic. Doing so should be avoided at all cost. Using
Try/Catch is very inneficient and can make applications perform very slowy.
Aidy's example utilizing TryParse is an incredibly more efficient way to
handle this.
In this case either way is valid. A non-integer value might very well be
considered to be an exceptional situation. In that case performance is
not an issue. It's true that exceptions should not be used in normal
program flow, but if the validation fails, the normal program flow is
out of the picture anyway.
-
The catch, however, should _not_ catch any exception, it should catch
only FormatException and OverflowException. Don't catch exceptions that
you don't know how to handle.
Also the code handling the exception should _not_ simply do a return, it
should actually handle the situation.
--
Göran Andersson
_____
http://www.guffa.com