Okay, I do see ToInt32() has an Exception section.
And I'll use TryParse().
Thanks.
--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
"Morten Wennevik [C# MVP]" <(E-Mail Removed)> wrote in message
news

(E-Mail Removed)...
Hi Jonathan,
The documentation say what kind of exceptions they might throw. Depending
on what data type you pass to Convert.ToInt32 you might get an
OverflowException, InvalidCastException, ArgumentException or
FormatException, although to know this you have to read the documentation
on every overloaded method. Knowing that Request.QueryString[string] will
return a string narrows it down to a FormatException or OverflowException
For primitive data types there is also a TryParse method that will not
throw an exception, but return true or false to indicate if the parse was
successfull.
int id = 0;
if(!Int32.TryParse(Request.QueryString["id"], out id))
{
// not a valid integer
}
On Wed, 14 Feb 2007 06:36:12 +0100, Jonathan Wood <(E-Mail Removed)>
wrote:
> I must be the only one who doesn't think exceptions are the greatest
> thing
> since bread.
>
> Consider the following code:
>
> id = Convert.ToInt32(Request.QueryString["id"]);
>
> First, is there an easy way to tell which methods or properties could
> potentially throw an exception? I've scanned the docs and it doesn't seem
> to
> say. Without that knowledge, it's a bit of hit and miss.
>
> Second, knowing that ToInt32() can throw an exception, is there any way
> to
> prevent that? The line above already appears within a try...catch.
> However,
> I don't want the same catch handler to handle problems with this line.
> I'd
> like it handled differently. What other choice is there besides creating
> an
> additional try and/or catch block? And doesn't that seem like overkill?
>
> Thanks.
>
--
Happy Coding!
Morten Wennevik [C# MVP]