避免例外 [英] Avoiding Exceptions

查看:93
本文介绍了避免例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须是唯一一个不认为异常是最棒的东西

自面包以来。


请考虑以下代码:


id = Convert.ToInt32(Request.QueryString [" id"]);


首先,有一种简单的方法可以告诉哪些方法或属性可能

可能抛出异常?我已经扫描了文档,而且似乎没什么好说的。没有这些知识,它有点受欢迎。


其次,知道ToInt32()可以抛出异常,有没有办法来支付
防止这种情况?上面的一行已经出现在try ... catch中。但是,

我不想要相同的catch处理程序来处理这一行的问题。我会以不同的方式处理
。除了创建一个额外的尝试和/或捕获块之外还有什么其他选择?这看起来有点矫枉过正吗?


谢谢。


-

乔纳森伍德

SoftCircuits编程
http://www.softcircuits.com

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.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

推荐答案

Jonathan,
Jonathan,

>其次,知道ToInt32()可以抛出异常,有什么办法可以防止这种情况发生吗?
>Second, knowing that ToInt32() can throw an exception, is there any way to
prevent that?



如果您的目标是.NET 2.0,请考虑使用Int32.TryParse而不是

Convert.ToInt32。

Mattias


-

Mattias Sj?gren [C#MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。

If you''re targeting .NET 2.0, consider using Int32.TryParse instead of
Convert.ToInt32.
Mattias

--
Mattias Sj?gren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


嗨Jonathan,


文档说明他们可能会抛出什么样的例外情况。取决于您传递给Convert.ToInt32的数据类型,你可能得到一个

溢出异常,InvalidCastException,ArgumentException或

FormatException,虽然要知道这个你必须阅读每个重载方法的文档

。知道Request.QueryString [string]将

返回一个字符串,将其缩小为FormatException或OverflowException


对于原始数据类型,还有一个TryParse方法那不会是
抛出一个异常,但返回true或false来表明该解析是否是

成功。


int id = 0;

if(!Int32.TryParse(Request.QueryString [" id"],out id))

{

/ /不是有效整数

}

2007年2月14日星期三06:36:12 +0100,Jonathan Wood< jw *** @ softcircuits.com

写道:
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 <jw***@softcircuits.com
wrote:

我必须是唯一一个不认为异常是最好的人b / b


自面包。


请考虑以下代码:


id = Convert.ToInt32(Request.QueryString [" ; id"]);


首先,是否有一种简单的方法可以告诉哪些方法或属性可以

potent我会抛出异常?我已经扫描过这些文档了,并且它似乎没有b $ b b / b
。如果没有这方面的知识,那就有点受伤了。


其次,知道ToInt32()可以抛出异常,有什么办法



防止这种情况?上面的一行已经出现在try ... catch中。

然而,

我不想让同一个捕获处理程序来处理这一行的问题。

我会

喜欢它的处理方式不同。还有什么其他的选择除了创造



额外的尝试和/或捕获块?这看起来有点矫枉过正吗?


谢谢。
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]



--
Happy Coding!
Morten Wennevik [C# MVP]


在2.0中的Int.TryParse()之前,我做了类似下面的事情:


公共静态类FailSafeConvert

{

public static int ConvertToInt(object value,int defaultValue)

{

试试

{

Convert.ToInt32(value);

}

catch

{

返回defaultValue;

}

}

}


我没有编码器,如果Convert.ToInt32没有拿一个物体,

然后我知道我有一个类似的方法ConvertToString()我可能有

先将值包起来。


问候,


- 保罗。


" Jonathan Wood" < jw *** @ softcircuits.com写信息

新闻:%2 **************** @ TK2MSFTNGP03.phx.gbl ...
Before Int.TryParse() in 2.0, I did something like below:

public static class FailSafeConvert
{
public static int ConvertToInt( object value, int defaultValue)
{
try
{
Convert.ToInt32(value);
}
catch
{
return defaultValue;
}
}
}

I don''t have the coder to hand, if Convert.ToInt32 doesn''t take an Object,
then I know I had a similar method ConvertToString() which I may have
wrapped the value up in first.

Regards,

- Paul.

"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...

>我必须是唯一一个不认为异常是最好的东西
自面包以来。


考虑以下代码:


id = Convert.ToInt32(Request.QueryString [" id"]);


First ,有没有一种简单的方法可以告诉哪些方法或属性可能会抛出异常?我已经扫描过这些文档而且看起来并不是很好。没有这些知识,它有点受欢迎。


其次,知道ToInt32()可以抛出异常,有没有办法来支付
防止这种情况?上面的行已经出现在try ... catch。

但是,我不希望同一个catch处理程序来处理这个

行的问题。我希望它的处理方式不同。还有什么其他选择

创建额外的try和/或catch块?并且看起来不是很好吗

矫枉过正?


谢谢。


-

Jonathan Wood

SoftCircuits编程
http:// www.softcircuits.com



这篇关于避免例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆