为什么try语句(和catch子句)需要一个块? [英] Why does the try statement (and catch clause) require a block?
问题描述
允许以下内容:
if(a == b)
SomeFunction();
else
OtherFunction();
以下不是:
尝试
DoSomething ();
catch(例外e)
ProcessError(e);
我检查了C#语法并确认了,与所有其他
复合语句不同,它需要一个块 - 即:需要开口{和
关闭}。
我只是想要能够写下面的内容(避免太多
缩进):
尝试
使用(FileStream file = new FileStream(" someFile.txt",
FileMode.OpenOrCreate))
使用(SomeResource rsrc = new SomeResource())
{
//在这里做东西
}
catch(例外e)
{
//在这里处理错误
}
re **** @ gmail.com 写道:
允许以下内容:
if(a == b)
SomeFunction();
else
OtherFunction();
以下不是:
尝试
DoSomething();
catch(例外e)
ProcessError(e);
我最好的猜测(这只是一个猜测)是试用块不是免费的b / b
语言设计师希望它们脱颖而出。
-
..NET 2.0 for Delphi程序员
www.midnightbeach.com/.net
您需要了解的内容。
Jon Shemitz写道:
re **** @ gmail.com 写道:
>允许以下内容:
if(a == b)
SomeFunction();
OtherFunction();
以下不是:
尝试
DoSomething();
catch(例外e)
ProcessError(e);
我最好的猜测(这只是一个猜测)是试用块不是免费的,而且语言设计师希望它们是站出来。
此外,try块和catch块都必须构成
唯一范围 - 内部声明的变量在外部不可见(也不是如果允许单一陈述,他们将是b $ b。出于这个原因,它需要使用大括号使其成为一个正式的块。
-cd
恕我直言,我相信这是风格和历史的结合。风格是主观的,我不会采取一种方式或者另一种方式来确定它是否是正确的。但是,C#的一个特性是它是C和C ++系列语言的演变。由于C ++ try块具有
花括号,C#使C ++程序员的迁移路径更容易。
你会发现C#语法在很多方面类似于C ++。
那就是说,我认为有一个技术原因要求C#设计为这样的b $ b因为VB.NET尝试阻止唐没有开头/结尾,这是一个没有它的语法示例。
Joe
-
http://www.csharp-station.com >
" re **** @ gmail.com"写道:
允许以下内容:
if(a == b)
SomeFunction();
else
OtherFunction();
以下不是:
尝试
DoSomething();
catch(例外e)
ProcessError(e);
我检查了C#语法并且它确认了,与所有其他
复合语句不同,它需要一个块 - 即:开头{和
结束需要。
我只想写下面的内容(以避免太多
缩进):
尝试
使用(FileStream file = new FileStream(" someFile.txt",
FileMode.OpenOrCreate))
using(SomeResource rsrc =新的SomeResource())
{
//在这里做东西
}
catch(例外e)
{
//这里处理错误
}
While the following is allowed:
if (a == b)
SomeFunction();
else
OtherFunction();
The following is not:
try
DoSomething();
catch (Exception e)
ProcessError(e);
I checked the C# grammar and it confirms that, unlike all other
compound statements, it requires a block - ie: the opening { and
closing } are required.
I just want to be able to write the following (to avoid too many
indents):
try
using (FileStream file = new FileStream("someFile.txt",
FileMode.OpenOrCreate))
using (SomeResource rsrc = new SomeResource())
{
// do stuff here
}
catch (Exception e)
{
// handle error here
}
re****@gmail.com wrote:
While the following is allowed:
if (a == b)
SomeFunction();
else
OtherFunction();
The following is not:
try
DoSomething();
catch (Exception e)
ProcessError(e);My best guess (and it IS just a guess) is that try blocks are not
free, and the language designers wanted them to stand out.
--
..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
Jon Shemitz wrote:re****@gmail.com wrote:
>While the following is allowed:
if (a == b)
SomeFunction();
else
OtherFunction();
The following is not:
try
DoSomething();
catch (Exception e)
ProcessError(e);
My best guess (and it IS just a guess) is that try blocks are not
free, and the language designers wanted them to stand out.In addition, both the try block and the catch block by necessity constitute
unique scopes - variables declared inside are not visible outside (nor would
they be if a single statement were allowed). For that reason, it makes
sense to require the braces to make it a formal block.
-cd
IMHO, I believe this is a combination of style and history. Style is
subjective and I don''t take a side one way or the other as to whether it is
correct or not. However, one of the features of C# is that it is an
evolution of the C and C++ family of languages. Since the C++ try block has
curly braces, C# makes the migration path easier for the C++ programmer.
You''ll find that C# syntax is similar in many ways to C++.
That said, I don''t think there is a technical reason requiring that C# be
designed that way because VB.NET try blocks don''t have begin/end, which is an
example of syntax that works without it.
Joe
--
http://www.csharp-station.com
"re****@gmail.com" wrote:
While the following is allowed:
if (a == b)
SomeFunction();
else
OtherFunction();
The following is not:
try
DoSomething();
catch (Exception e)
ProcessError(e);
I checked the C# grammar and it confirms that, unlike all other
compound statements, it requires a block - ie: the opening { and
closing } are required.
I just want to be able to write the following (to avoid too many
indents):
try
using (FileStream file = new FileStream("someFile.txt",
FileMode.OpenOrCreate))
using (SomeResource rsrc = new SomeResource())
{
// do stuff here
}
catch (Exception e)
{
// handle error here
}
这篇关于为什么try语句(和catch子句)需要一个块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!