异常处理建议 [英] Exception handling suggestions

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

问题描述

好的像int.Parse(string)之类的东西可以抛出这些异常:

ArgumentNullException,FormatException,OverflowException


我不想要我的程序只是崩溃异常,所以我必须

处理所有这些。我不在乎发生了哪一个,除了

写出异常。消息到日志文件。把三个处理程序写成

似乎都很啰嗦。所以,我可以抓住

例外。但是,这是推荐的吗?它会捕获所有内容,甚至是内存异常的



你有什么建议?


Zytan

解决方案

main

{

AppDomain.CurrentDomain.UnhandledException + =新的

UnhandledExceptionEventHandler(CurrentDomain_Unhan dledException);

Application.Run(new MyFirstForm());

}


private static void CurrentDomain_UnhandledException(object sender,

UnhandledExceptionEventArgs e)

{

//应用程序关闭..,你可以在这里登录..

//日志退出后

Application.Exit();


}


这应该照顾它..

VJ


" Zytan" < zy ********** @ yahoo.comwrote in message

news:11 ******************** **@q40g2000cwq.googlegr oups.com ...


好​​像int.Parse(string)之类的东西可以抛出这些异常:

ArgumentNullException,FormatException,OverflowException


我不希望我的程序崩溃异常,所以我必须

处理所有这些。我不在乎发生了哪一个,除了

写出异常。消息到日志文件。把三个处理程序写成

似乎都很啰嗦。所以,我可以抓住

例外。但是,这是推荐的吗?它会捕获所有内容,甚至是内存异常的



你有什么建议?


Zytan



main


{

AppDomain.CurrentDomain.UnhandledException + = new

UnhandledExceptionEventHandler(CurrentDomain_Unhan dledException);

Application.Run(new MyFirstForm());

}


private static void CurrentDomain_UnhandledException(object sender,

UnhandledExceptionEventArgs e)

{

//应用程序关闭..,你可以在这里登录..

//登录后退出

Application.Exit();

}



哇,谢谢,VJ。好的,什么是应用程序?这是你的名字吗?b $ b?我在一个控制台应用程序中测试它,它不存在。


另外,我实际上无法测试此代码,因为调试器总是停止

关于未处理异常的程序。你是如何测试这个的?


实际上,我在考虑如何处理异常,其中我不希望程序退出?例如,对于int.Parse(),我只是

想要捕获所有内容,并保持程序运行,因为传递给Parse()的数据可能是
不合适,所以我想抓住它,

并通知我,但继续运行。抓住Exception真是个好主意吗?


Zytan


Zytan写道:


好​​像int.Parse(string)之类的东西可以抛出这些异常:

ArgumentNullException,FormatException,OverflowException


我不希望我的程序崩溃异常,所以我必须

处理所有这些。我不在乎发生了哪一个,除了

写出异常。消息到日志文件。把三个处理程序写成

似乎都很啰嗦。所以,我可以抓住

例外。但是,这是推荐的吗?它会捕获所有内容,甚至是内存异常的



你有什么建议?


Zytan



如果字符串真的可以为null,那么很容易检查。你应该在调用方法之前检查它,而不是等待异常。


如果你真的不需要确定为什么字符串

无法转换,您可以使用int.TryParse方法。然后

你根本不需要任何异常处理。


-

G?跑Andersson

_____
http://www.guffa.com


Ok something simple like int.Parse(string) can throw these exceptions:
ArgumentNullException, FormatException, OverflowException

I don''t want my program to just crash on an exception, so I must
handle all of them. I don''t care about which one happened, except to
write out exception.Message to a log file. It seems verbose to write
out three handlers that all do the same thing. So, I could just catch
Exception. But, is that recommended? It''ll catch everything, even
out of memory exceptions.

What do you suggest?

Zytan

解决方案

main
{
AppDomain.CurrentDomain.UnhandledException+=new
UnhandledExceptionEventHandler(CurrentDomain_Unhan dledException);
Application.Run(new MyFirstForm());
}

private static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
// The app shuts down.., you can log here..
//After log exit
Application.Exit();

}

That should take care of it..
VJ

"Zytan" <zy**********@yahoo.comwrote in message
news:11**********************@q40g2000cwq.googlegr oups.com...

Ok something simple like int.Parse(string) can throw these exceptions:
ArgumentNullException, FormatException, OverflowException

I don''t want my program to just crash on an exception, so I must
handle all of them. I don''t care about which one happened, except to
write out exception.Message to a log file. It seems verbose to write
out three handlers that all do the same thing. So, I could just catch
Exception. But, is that recommended? It''ll catch everything, even
out of memory exceptions.

What do you suggest?

Zytan



main

{
AppDomain.CurrentDomain.UnhandledException+=new
UnhandledExceptionEventHandler(CurrentDomain_Unhan dledException);
Application.Run(new MyFirstForm());
}

private static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
// The app shuts down.., you can log here..
//After log exit
Application.Exit();
}

Wow, thanks, VJ. Ok, so what is Application? Is that a name you
made? I am testing this in a console app, and it doesn''t exist.

Also, I can''t actually test this code, since the debugger always halts
the program on an unhandled exception. How do you test this?

Actually, I was thinking about how do I handle exceptions in which I
don''t want the program to quit? For example, for int.Parse(), I just
want to catch everything, and keep the program running, since the data
passed into Parse() could possible be improper, so I want to catch it,
and notify me, but continue running. Is catching Exception really a
good idea?

Zytan


Zytan wrote:

Ok something simple like int.Parse(string) can throw these exceptions:
ArgumentNullException, FormatException, OverflowException

I don''t want my program to just crash on an exception, so I must
handle all of them. I don''t care about which one happened, except to
write out exception.Message to a log file. It seems verbose to write
out three handlers that all do the same thing. So, I could just catch
Exception. But, is that recommended? It''ll catch everything, even
out of memory exceptions.

What do you suggest?

Zytan

If the string really can be null, that is easy to check for. You should
check that before calling the method instead of waiting for the exception.

If you don''t really need to determine the exact reason why the string
can''t be converted, you can use the int.TryParse method instead. Then
you will not need any exception handling at all.

--
G?ran Andersson
_____
http://www.guffa.com


这篇关于异常处理建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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