编程风格的问题 [英] A question of programming style
问题描述
嗨
这是一个关于编程风格和某个
范围垃圾收集的相当通用的问题。我有一些方法可以写入文本日志,
事件日志等在方法的不同位置。我所做的是在方法开始时打开
各种日志,并且很好,我最后明确关闭了
。在该方法中,我还在不同的地方捕获异常。
对于其中一些异常,我希望退出该方法。现在很迂腐,
即使在catch
子句中,我也想在退出方法之前关闭日志。所以我最终得到的结果如下:
private void myMethod()
{
EventLog log = new EventLog( strMyLog);
log.Source = strMySource;
//此处的主要代码块,后跟...
try
{
//一些可能抛出异常的代码
}
catch
{
//这里有一些异常代码
log.Dispose();
返回;
}
//位模式代码在这里做东西
尝试
{
//更多代码可能会抛出异常
}
catch
{
//这里有更多异常代码
log.Dispose();
返回;
}
//这里的最终代码
log.Dispose();
}
虽然这有效并迫使我自己做整理,但它似乎是
a有点笨重。所以我的问题是:
- 我是否只是不打算整理,只是让垃圾
收藏家排序一切? (这违背了粮食!)
- 有没有更好的方法来组织代码,这样我只有一个
log.Dispose()?
- 也许我应该将整个方法包装在try finally子句中? (这个
似乎是一个奇怪的选择。)
- 我只是担心什么?
TIA
Mark
Hi
This is a fairly generic question about programming style and to a certain
extent garbage collection. I have some methods that I write to text logs,
the event log etc at various places in the method. What I do is open the
various logs at the start of the method and, being good, I explicitly close
them at the end. In the method, I also catch exceptions at various places.
For some of these exceptions, I wish to exit the method. Now being pedantic,
I also want to close the logs before exiting the method even in the catch
clause. So what I end up with is something like:
private void myMethod()
{
EventLog log = new EventLog(strMyLog);
log.Source = strMySource;
// main block of code here, followed by...
try
{
// some code that might throw an exception
}
catch
{
// some exception code here
log.Dispose();
return;
}
// bit mode code to do stuff here
try
{
// some more code that might throw an exception
}
catch
{
// more exception code here
log.Dispose();
return;
}
// final code stuff here
log.Dispose();
}
Whilst this works and forces me to do all the tidying up myself, it appears
a bit clunky. So my question is:
- Should I just not bother trying to tidy up and just let the garbage
collector sort everything out? (This goes against the grain!)
- Is there a better way of organizing the code so that I only have one
log.Dispose()?
- Perhaps I should wrap the whole method in a try finally clause? (This
seems a weird option.)
- Am I just worrying about nothing?
TIA
Mark
推荐答案
Mark< ma ** @ ReMoVeThIsBiTmossywell.com>写道:
Mark <ma**@ReMoVeThIsBiTmossywell.com> wrote:
这是一个关于编程风格和某种垃圾收集的相当普遍的问题。我有一些方法可以在方法的各个地方写入文本日志,事件日志等。我所做的是在方法开始时打开各种日志,并且好,我最后明确关闭它们。在该方法中,我还在各个地方捕获异常。
对于其中一些异常,我希望退出该方法。现在很迂腐,我还想在退出方法之前关闭日志,即使是在catch
子句中也是如此。所以我最终得到的是:
This is a fairly generic question about programming style and to a certain
extent garbage collection. I have some methods that I write to text logs,
the event log etc at various places in the method. What I do is open the
various logs at the start of the method and, being good, I explicitly close
them at the end. In the method, I also catch exceptions at various places.
For some of these exceptions, I wish to exit the method. Now being pedantic,
I also want to close the logs before exiting the method even in the catch
clause. So what I end up with is something like:
< snip>
首先,你可以使用finally块。这种事情恰好是一个终极块的目的。其次,你可以使用using(...)构造自动获得它b / b $ b。你的方法将成为:
private void myMethod()
{
使用(EventLog log = new EventLog(strMyLog))
{
log.Source = strMySource;
//这里的主要代码块,后跟......
尝试
{
//一些代码可能会抛出异常
}
catch
{
//这里有一些例外代码
返回;
}
//位模式代码在这里做东西
试试
{
//一些可能引发异常的代码
}
catch
{
//这里有更多例外代码
return;
}
}
}
-
Jon Skeet - < ; sk *** @ pobox.com>
http://www.pobox .com / ~siget
如果回复小组,请不要给我发邮件
<snip>
Firstly, you can use a finally block. This kind of thing is exactly
what a finally block is for. Secondly, you can get it automatically
with the using (...) construct. Your method would become:
private void myMethod()
{
using (EventLog log = new EventLog(strMyLog))
{
log.Source = strMySource;
// main block of code here, followed by...
try
{
// some code that might throw an exception
}
catch
{
// some exception code here
return;
}
// bit mode code to do stuff here
try
{
// some more code that might throw an exception
}
catch
{
// more exception code here
return;
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道
新闻:MP ************************ @ msnews.microsoft.c om ...
[snip]
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
[snip]
首先,你可以使用finally块。这种事情恰好是最终块的用途。
Firstly, you can use a finally block. This kind of thing is exactly
what a finally block is for.
感谢您的快速反应。我考虑过使用(如果你知道我的意思是什么意思!),但我经常会打开几个事件日志,有时会打开一些文件
(对不起,我的例子只显示了单个事件日志 - 我的错误)。所以,我可以将
单独包装在一个using语句中,但嵌套那个批次可能很麻烦?b $ b很麻烦?我喜欢使用finally子句的声音,但确切地说
会去哪里?我在方法的最后尝试了一下它自己,但是没有
令人惊讶的是它需要一个try子句!所以我想我真正追求的是
看起来像这样但当然没有goto:
private void myMethod()
{
log1.Source = strMySource1;
log2.Source = strMySource2;
StreamWriter sw1 = new StreamWriter( strMyFile1,true)
StreamWriter sw2 = new StreamWriter(strMyFile2,true)
//这里的主要代码块,后跟...
试试
{
//一些代码可能会抛出异常
}
catch
{
//这里有一些异常代码
goto TimeToGetOuttaHere;
}
>
//位模式代码在这里做东西
尝试
{
//一些可能引发异常的代码
}
catch
{
//这里有更多例外代码
转到TimeToGetOuttaHere;
}
//更多代码到这里
标签:Ti meTogetOuttahere
sw2.Close();
sw1.Close();
log2.Dispose();
log1.Dispose();
}
我敢肯定必须有一个简单的方法来实现这个目标而不需要大的
嵌套ifs,trys等的数量,但我现在看不到它!一些
类型的语句意味着在现有的
方法之前运行这段代码,即使我们已经在方法的其他地方点击了返回语句将
有用。
干杯
马克
Thanks for the rapid response. I considered using using (if you know what I
mean!), but often I open a couple of event logs and sometime a few files
(sorry my example only showed a single event log - my mistake). So, I could
wrap each individually in a using statement, but nesting that lot can be
cumbersome? I like the sound of using a finally clause, but where exactly
would it go? I tried a finally on it''s own at the end of the method but not
surprisingly it wants a try clause! So I guess that what I really after is
something that looks like this but of course without the "goto":
private void myMethod()
{
log1.Source = strMySource1;
log2.Source = strMySource2;
StreamWriter sw1 = new StreamWriter(strMyFile1, true)
StreamWriter sw2 = new StreamWriter(strMyFile2, true)
// main block of code here, followed by...
try
{
// some code that might throw an exception
}
catch
{
// some exception code here
goto TimeToGetOuttaHere;
}
// bit mode code to do stuff here
try
{
// some more code that might throw an exception
}
catch
{
// more exception code here
goto TimeToGetOuttaHere;
}
// More code goes here
Label: TimeTogetOuttahere
sw2.Close();
sw1.Close();
log2.Dispose();
log1.Dispose();
}
I''m sure that there must be an easy way to achieve this without large
numbers of nested ifs, trys etc, but I just can''t see it at the moment! Some
kind of statement that means "run this bit of code before existing the
method, even if we''ve hit a return statement elsewhere in the method" would
be useful.
Cheers
Mark
否攻击意图,但你需要阅读文档稍微有点
on try ... catch ... finally块以解释在哪里放置
最后一部分。
另外,你可以在using语句中指定多个对象,根据
doc示例:
using(Font MyFont = new Font(" Arial",10.0f),MyFont2 = new Font(" Arial",
10,0f))
{
}
或在你的情况下
使用(StreamWriter sw1 = new StreamWriter(strMyFile1,true),sw2 = new
StreamWriter(strMyFile2,true))
{
}
您可能还想看一下进入跟踪或调试监听器的概念。在我看来,
可以提供更优雅的解决方案,并且会添加
选项,以便能够通过
更改日志记录级别和事件日志
配置文件。然后你的代码只需说
Trace.WriteLine(blah blah blah);
它将写入所有配置的事件日志。
" Mark" <毫安** @ ReMoVeThIsBiTmossywell.com>在消息中写道
news:3f ********************* @ news.dial.pipex.com ..。
No offense intended, but methinks you need to read the documentation a bit
on try...catch...finally blocks for an explanation of where to put the
finally part.
Also, you can specify multiple objects in the using statement, according to
the doc example:
using (Font MyFont = new Font("Arial", 10.0f), MyFont2 = new Font("Arial",
10,0f))
{
}
or in your case
using (StreamWriter sw1 = new StreamWriter(strMyFile1, true), sw2 = new
StreamWriter(strMyFile2, true))
{
}
You may also want to look into the concept of trace or debug listeners. It
would make for a much more elegant solution in my opinion, and would add the
option of being able to change logging levels and event logs via a
configuration file. Then your code just needs to say
Trace.WriteLine("blah blah blah");
and it will be written to all the configured event logs.
"Mark" <ma**@ReMoVeThIsBiTmossywell.com> wrote in message
news:3f*********************@news.dial.pipex.com.. .
Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道
新闻:MP ************************ @ msnews.microsoft.c om ...
[snip] ]
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
[snip]
首先,你可以使用finally块。这种事情恰好是最后一块是什么。
感谢您的快速反应。我考虑过使用(如果你知道什么是
Firstly, you can use a finally block. This kind of thing is exactly
what a finally block is for.
Thanks for the rapid response. I considered using using (if you know what
我的意思!),但我经常会打开几个事件日志,有时会打开一些文件
(对不起我的例子只显示了单个事件日志 - 我的错误)。所以,我
可以将每个单独包装在一个using语句中,但是嵌套那个批次可能很麻烦?我喜欢使用finally子句的声音,但它究竟会在哪里?我在方法结束时尝试了它自己的终结,但是
并不奇怪它想要一个try子句!所以我想我真正追求的是
看起来像这样的东西但当然没有goto:
I mean!), but often I open a couple of event logs and sometime a few files
(sorry my example only showed a single event log - my mistake). So, I
could wrap each individually in a using statement, but nesting that lot can be
cumbersome? I like the sound of using a finally clause, but where exactly
would it go? I tried a finally on it''s own at the end of the method but
not surprisingly it wants a try clause! So I guess that what I really after is
something that looks like this but of course without the "goto":
这篇关于编程风格的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!