试试Catch Overkill [英] Try Catch Overkill

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

问题描述



这段代码是否代表完全矫枉过正??


试试

{
< br $>
//创建一个treenode

TreeNode tn = new TreeNode();

//将它添加到树视图

tv.Nodes.Add(tn);

//做一些其他的东西


}

catch(例外ee) //发生了一些奇怪的事情

{


试试

{

//试图删除树视图中的treenode

if(tn.TreeView!= null)tn.Remove();

}

catch(例外ee) //现在事情变得非常可怕

{

MessageBox.Show(&会像TreeNode.Remove()这样的函数会导致

a我应该担心的随机异常?");

}


}


我基本上是什么意思是......如果出现这种情况,那么有人可能会把一杯咖啡倒入电脑,所以我不必担心我的软件/数据是可恢复的,或者这种情况会不会发生?b / b

解释,沉思和批评都欢迎,

James Randle。

解决方案

你永远不应该在一个catch块中有另一个try块


" pigeonrandle" < pi ********** @ hotmail.comwrote in message

news:11 ******************** *@b28g2000cwb.googlegro ups.com ...




这段代码是否代表完全矫枉过正??


试试

{


//创建一个treenode

TreeNode tn = new TreeNode( );

//将它添加到树视图

tv.Nodes.Add(tn);

//做其他一些事情


}

catch(例外ee)//发生了奇怪的事情

{


尝试

{

//尝试从树视图中删除treenode

if(tn.TreeView!= null)tn.Remove( );

}

catch(例外ee)//现在事情真的很可怕

{

MessageBox .Show(像TreeNode.Remove()这样的函数会导致

a我应该担心的随机异常吗?;)

}
< br $>
}


我的基础cally意思是......如果出现这种情况,那么有人可能会把一杯咖啡倒入电脑,所以我不必担心我的软件/数据是可恢复的,或者这种情况会不会发生?b / b

解释,沉思和批评都欢迎,

詹姆斯·兰德尔。





肯定会在catch块中出现例外情况?如果发生这种情况,男人该怎么办?


干杯,

詹姆斯。


fallenidol写道:


你永远不应该在一个catch区块内有另一个try块


" pigeonrandle" < pi ********** @ hotmail.comwrote in message

news:11 ******************** *@b28g2000cwb.googlegro ups.com ...




这段代码是否代表完全矫枉过正??


试试

{


//创建一个treenode

TreeNode tn = new TreeNode( );

//将它添加到树视图

tv.Nodes.Add(tn);

//做其他一些事情


}

catch(例外ee)//发生了奇怪的事情

{


尝试

{

//尝试从树视图中删除treenode

if(tn.TreeView!= null)tn.Remove( );

}

catch(例外ee)//现在事情真的很可怕

{

MessageBox .Show(像TreeNode.Remove()这样的函数会导致

a我应该担心的随机异常吗?;)

}


}


我的意思是......如果出现这种情况,那么有人可能会把一杯咖啡倒进电脑,所以我不会有

担心我的软件/数据是可恢复的,或者这种情况会不会发生?*
*搞笑的事情?


解释,沉思和批评都欢迎,

James Randle。


" fallenidol" < fa ******** @ discussion.microsoft.com写信息

新闻:e3 ************** @ TK2MSFTNGP03.phx.gbl ...


你永远不应该在一个catch块中有另一个try块



哦,真的吗?而你的基础是什么?如果您的异常处理

调用另一个可能导致异常的函数怎么办?你要
应该让它一直浮到操作系统吗?


-

Adam Clauss


Hi,
Does this bit of code represent complete overkill?!

try
{

//create a treenode
TreeNode tn = new TreeNode();
//add it to a treeview
tv.Nodes.Add(tn);
//do some other stuff

}
catch (Exception ee) //something weird has happened
{

try
{
//try to remove the treenode from the treeview
if (tn.TreeView != null) tn.Remove();
}
catch (Exception ee) //now things have got really dire
{
MessageBox.Show("Would a function like TreeNode.Remove() ever cause
a random exception that i should worry about?");
}

}

What i basically mean is ... if this situation arises then someone has
probably poured a cup of coffee into the computer so i won''t have to
worry about my software/data being ''recoverable'', or will this sort of
*hilarious* thing never happen?

Explanations, musings and criticism all welcome,
James Randle.

解决方案

you should never have another try block within a catch block

"pigeonrandle" <pi**********@hotmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...

Hi,
Does this bit of code represent complete overkill?!

try
{

//create a treenode
TreeNode tn = new TreeNode();
//add it to a treeview
tv.Nodes.Add(tn);
//do some other stuff

}
catch (Exception ee) //something weird has happened
{

try
{
//try to remove the treenode from the treeview
if (tn.TreeView != null) tn.Remove();
}
catch (Exception ee) //now things have got really dire
{
MessageBox.Show("Would a function like TreeNode.Remove() ever cause
a random exception that i should worry about?");
}

}

What i basically mean is ... if this situation arises then someone has
probably poured a cup of coffee into the computer so i won''t have to
worry about my software/data being ''recoverable'', or will this sort of
*hilarious* thing never happen?

Explanations, musings and criticism all welcome,
James Randle.



Hi,
Surely exceptions can occur in catch blocks? And what is a man to do if
this happens?!

Cheers,
James.

fallenidol wrote:

you should never have another try block within a catch block

"pigeonrandle" <pi**********@hotmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...

Hi,
Does this bit of code represent complete overkill?!

try
{

//create a treenode
TreeNode tn = new TreeNode();
//add it to a treeview
tv.Nodes.Add(tn);
//do some other stuff

}
catch (Exception ee) //something weird has happened
{

try
{
//try to remove the treenode from the treeview
if (tn.TreeView != null) tn.Remove();
}
catch (Exception ee) //now things have got really dire
{
MessageBox.Show("Would a function like TreeNode.Remove() ever cause
a random exception that i should worry about?");
}

}

What i basically mean is ... if this situation arises then someone has
probably poured a cup of coffee into the computer so i won''t have to
worry about my software/data being ''recoverable'', or will this sort of
*hilarious* thing never happen?

Explanations, musings and criticism all welcome,
James Randle.


"fallenidol" <fa********@discussions.microsoft.comwrote in message
news:e3**************@TK2MSFTNGP03.phx.gbl...

you should never have another try block within a catch block

Oh really? And you are basing that on? What if your exception handling
calls another function that could potentially cause an exception? You
should just let it float all the way up to the OS?

--
Adam Clauss


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

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