try-catch阻止了一种防止崩溃的方法吗? [英] Is try-catch block a way to prevent a crash?

查看:271
本文介绍了try-catch阻止了一种防止崩溃的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我发现如果我用下面说明的

方式的try-catch块包装我的代码,那么'我会执行catch

部分中指定的内容而不是崩溃。任何人都可以确认这是真的吗?


如果这是真的,我会修改程序中的每一个方法来包装它

with一个try-catch块来防止它崩溃。


FYI,我提到的try-catch块如下:


void SomeMethod ()

{

试试

{

//做点什么


}

catch(例外e)

{

logger.Write(e.Message);

}


}

Hi,

I''ve found that if I wrap my code with a try-catch block in the
fashion illustrated below, it''ll do what is specified in the catch
section instead of crashing. Could anyone confirm that this is true?

If it''s true, I''ll modify every single method in my program to wrap it
with a try-catch block to prevent it from crashing.

FYI, the try-catch block I mentioned is as below:

void SomeMethod()
{
try
{
// Do something

}
catch (Exception e)
{
logger.Write (e.Message);
}

}

推荐答案

2008年6月12日星期四19:12:36 -0700( PDT),好奇

< fi ******** @ yahoo.comwrote:
On Thu, 12 Jun 2008 19:12:36 -0700 (PDT), Curious
<fi********@yahoo.comwrote:

>

我发现如果我用下面说明的
方式用try-catch块包装我的代码,它会执行catch
部分中指定的内容崩溃任何人都可以确认这是真的吗?

如果确实如此,我会修改程序中的每一个方法,用一个try-catch块来包装它以防止它崩溃。

仅供参考,我提到的try-catch块如下:

void SomeMethod()
{

试试

{

//做点什么


}

catch(例外e)

{

logger.Write(e.Message);

}

}
>Hi,

I''ve found that if I wrap my code with a try-catch block in the
fashion illustrated below, it''ll do what is specified in the catch
section instead of crashing. Could anyone confirm that this is true?

If it''s true, I''ll modify every single method in my program to wrap it
with a try-catch block to prevent it from crashing.

FYI, the try-catch block I mentioned is as below:

void SomeMethod()
{
try
{
// Do something

}
catch (Exception e)
{
logger.Write (e.Message);
}

}



是的,这就是Try / Catch所做的。


但是,包装这样的每一种方法都可能真的很糟糕。理念。如果该方法的调用者希望该方法能够执行某些操作,那么当它无法执行任何操作时会发生什么,或者

可能更糟糕,那会怎么样?它。

Yes, that is what Try/Catch does.

However, wrapping every single method like that is probably a really
bad idea. What if the caller of the method expects the method to do
something, what will happen when it either doesn''t do anything, or
maybe worse, does half of it.


嗨杰克,


我明白了。我会仔细研究每种方法,以确保它们能够完成他们应该做的事情。


我将用方法包装一个try-catch块,以防止任何崩溃。
Hi Jack,

I get the idea. I''ll look at each method carefully to make sure they
do what they''re supposed to.

I''ll wrap the methods with a try-catch block to prevent any crash.


6月13日,8:32 * am,Curious< fir5tsi ... @ yahoo.comwrote:
On Jun 13, 8:32*am, Curious <fir5tsi...@yahoo.comwrote:

嗨杰克,


我明白了。我会仔细研究每种方法,以确保它们能够完成他们应该做的事情。


我将用方法包装一个try-catch块来防止任何崩溃。
Hi Jack,

I get the idea. I''ll look at each method carefully to make sure they
do what they''re supposed to.

I''ll wrap the methods with a try-catch block to prevent any crash.



你应该先读这个:

http://msdn.microsoft.com/en-us/libr...exception.aspx


Marc
http://nomagichere.blogspot.com


这篇关于try-catch阻止了一种防止崩溃的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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