开发者

System.Exception or just Exception

im a beginner at programming and to be honest everything I learn I want to make sure I do it correctly. My question is, on the MSDN site, it illustrates that when handling exceptions I must use the "System.foo", for example:

catch (System.DllNotFoundException ex)

But a lot of programmers around me and that would tend to use the exceptions without the heading, for example:

catch  (DllNotFoundException ex)

I know this may be trivial, and perhaps very dumb of me to ask but is there any real difference in these two implementations? like which of the two is better practice?

I appreciate any advic开发者_如何学Goe,

Thanks in advance.


using System;
using System.IO;

...
catch (DllNotFoundException)
{
}
catch (IOException)
{
}

or

// not using that namespaces

...
catch (System.DllNotFoundException)
{
}
catch (System.IO.IOException)
{
}

So there's no difference. Depends only on what exactly namespaces you have imported.


They are different ways of expressing exactly the same thing. DllNotFoundException is part of the System namespace which is usually included with a using System; statement at the top of the file by default. System.DllNotFoundException is just it's full type name.

By putting a using System; at the top of the file you're basically creating a shortcut for any System.* classes your reference in that file. E.g. You could but you do not need to specify the System namespace. Conversely, you could do without using System; or any other using but then you would have refer to every type in your file by its full name.


There's no difference, it's just a style difference. In the second example you will need to have a using System; statement to tell the compiler where to find the DllNotFoundException.


System is a namespace. Namespaces are used to organize your code and cope with naming issues.

The class DllNotFoundException is part of the System namespace so its full name is System.DllNotFoundException.

If you include a using System;into the header of your file you can use all classes from System namespace without specifying the full name, resulting in DllNotFoundException.


Using System.Exception is specific, because you can have the same names in different libraries, you can have specify exactly which one you wanted. Ergo, most likely its the same, but it means if there was a choice, there is no ambiguity.


In .NET namespaces are used to organize types. You can access these types by using their full name in this case System.DllNotFoundException, or when using the using statement - in this case using System, you can then omit the System dot prefix for all top level System types.

In answer to your question I think you should use using as it generally makes your code cleaner as it's less to type and read for types used.

When using multiple usings there can be ambiguities of the types used for example if DllNotFoundException existed in System and Foo where using System and using Foo was typed....

When there are ambiguities (the compiler will complain) you can either specify the whole type name or add a using MyException = Foo.DllNotFoundException, then MyException can be used instead of Foo.DllNotFoundException bad name example I know :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜