Luke Smith Write today, wrong tomorrow

Sometimes you have to catch an exception

I had a comment left recently on a post I made a few months ago (HttpRequest QueryValue extension methods), I was going to reply in the comments but felt it deemed a post in itself.

I agree 100% with the commenter about avoiding throwing exceptions and I try to always use the Tester-Doer pattern. As you can see in the code in the post I am using this pattern.

However the ConvertFromInvariantString method will throw an exception in cases where 'asdf' is trying to be converter to an int, and at the time I couldn't find a way to do something similar to a TryParse with a TypeConverter. The IsValid method exists but this always returns true and Microsoft have said they won't fix it. So using a TypeConverter as above you are required to catch an exception (sucks doesn't it?).

The IsValid Documentation states 'You can write your own WillConvertSuceed method by wrapping the ConvertTo and ConvertFrom methods in exception blocks.'.

If anyone knows of a way around having to catch an exception when using a TypeConverter please send me a message or leave a comment.

comments powered by Disqus