如何在 C# 中使用 Java 风格的 throws 关键字?
In Java, the throws
keyword allows for a method to declare that it will not handle an exception on its own, but rather throw it to the calling method.
Is there a similar keyword/attribute in C#?
If there is no equivalent, how can you accomplish the same (or a similar) effect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
该操作正在询问 Java 的 的 C# 等效项
throws
子句 - 不是throw
关键字。这用在 Java 的方法签名中,表示可以抛出已检查的异常。在 C# 中,没有与 Java 检查异常直接等效的东西。 C# 没有等效的方法签名子句。
翻译为
The op is asking about the C# equivalent of Java's
throws
clause - not thethrow
keyword. This is used in method signatures in Java to indicate a checked exception can be thrown.In C#, there is no direct equivalent of a Java checked exception. C# has no equivalent method signature clause.
translates to
在 Java 中,您必须处理异常或使用
throws
关键字将方法标记为可能抛出异常的方法。C# 没有这个关键字或等效的关键字,就像在 C# 中一样,如果不处理异常,它就会冒泡,直到被捕获,或者如果没有被捕获,它将终止程序。
如果你想处理它然后重新抛出,你可以执行以下操作:
In Java, you must either handle an exception or mark the method as one that may throw it using the
throws
keyword.C# does not have this keyword or an equivalent one, as in C#, if you don't handle an exception, it will bubble up, until caught or if not caught it will terminate the program.
If you want to handle it then re-throw you can do the following:
是的,这是一个旧线程,但是当我在谷歌上搜索答案时,我经常找到旧线程,所以我想我会添加一些我发现的有用的东西。
如果您使用的是 Visual Studio 2012,则有一个内置工具可用于允许 IDE 级别的“抛出”等效项。
如果您使用 XML 文档注释(如上所述),那么您可以使用 标记指定方法或类引发的异常类型以及有关何时或为什么它被抛出。
例子:
Yes this is an old thread, however I frequently find old threads when I am googling answers so I figured I would add something useful that I have found.
If you are using Visual Studio 2012 there is a built in tool that can be used to allow for an IDE level "throws" equivalent.
If you use XML Documentation Comments, as mentioned above, then you can use the <exception> tag to specify the type of exception thrown by the method or class as well as information on when or why it is thrown.
example:
这是我刚刚在 上找到的类似问题的答案bytes.com:
Here is an answer to a similar question I just found on bytes.com:
在浏览完这里的大部分答案后,我想补充一些想法。
依赖 XML 文档注释并期望其他人依赖是一个糟糕的选择。我遇到的大多数 C# 代码都没有完整地记录方法,并且与 XML 文档注释保持一致。然后还有一个更大的问题,如果 C# 中没有检查异常,您如何记录您的方法抛出的所有异常,以便 API 用户知道如何单独处理它们?请记住,您只知道在实现中使用 throw 关键字自己抛出的那些。您在方法实现中使用的 API 也可能会引发您不知道的异常,因为它们可能没有记录在案,并且您没有在实现中处理它们,因此它们会在您的调用者面前爆炸。方法。换句话说,这些 XML 文档注释并不能替代已检查的异常。
Andreas 在此处的答案中链接了对 Anders Hejlsberg 的采访,了解 C# 设计团队为何决定反对检查异常。对原始问题的最终回答隐藏在该采访中:
换句话说,没有人应该对特定 API 会出现什么样的异常感兴趣,因为您总是会在任何地方捕获所有异常。如果您想真正关心特定的异常,那么如何处理它们取决于您,而不是由某人使用 Java throws 关键字定义方法签名,从而强制 API 用户进行特定的异常处理。
——
就我个人而言,我对此感到困惑。我同意 Anders 的观点,即如果不添加新的不同问题,检查异常并不能解决问题。就像 XML 文档注释一样,我很少看到 C# 代码将所有内容都包含在 try finally 块中。在我看来,这确实是你唯一的选择,而且似乎是一个很好的做法。
After going through most of the answers here, I'd like to add a couple of thoughts.
Relying on XML Documentation Comments and expecting others to rely on is a poor choice. Most C# code I've come across does not document methods completely and consistently with XML Documentation Comments. And then there's the bigger issue that without checked exceptions in C#, how could you document all exceptions your method throws for the purpose of your API user to know how to handle them all individually? Remember, you only know about the ones you throw yourself with the throw keyword in your implementation. APIs you're using inside your method implementation might also throw exceptions that you don't know about because they might not be documented and you're not handling them in your implementation, so they'll blow up in face of the caller of your method. In other words, these XML documentation comments are no replacement for checked exceptions.
Andreas linked an interview with Anders Hejlsberg in the answers here on why the C# design team decided against checked exceptions. The ultimate response to the original question is hidden in that interview:
In other words, nobody should be interested in what kind of exception can be expected for a particular API as you're always going to catch all of them everywhere. And if you want to really care about particular exceptions, how to handle them is up to you and not someone defining a method signature with something like the Java throws keyword, forcing particular exception handling on an API user.
--
Personally, I'm torn here. I agree with Anders that having checked exceptions doesn't solve the problem without adding new, different problems. Just like with the XML documentation comments, I rarely see C# code with everything wrapped in try finally blocks. It feels to me though this is indeed your only option and something that seems like a good practice.
实际上,C# 中没有检查异常可以被认为是一件好事,也可以被认为是一件坏事。
我自己认为这是一个很好的解决方案,因为检查的异常给您带来了以下问题:
因此,在大多数较大的应用程序中,当发生受检查的异常时,您经常会看到以下模式:
这本质上意味着模拟 C#/.NET 处理所有异常的方式。
Actually not having checked exceptions in C# can be considered a good or bad thing.
I myself consider it to be a good solution since checked exceptions provide you with the following problems:
Because of that in most bigger applications you will see the following pattern often when checked Exceptions occur:
Which essentially means emulating the way C#/.NET handles all Exceptions.
您询问的是:
重新抛出异常
或
You are asking about this :
Re-throwing an Exception
or
.Net CodeContract
EnsuresOnThrow>>< 之间存在一些短暂的相似之处/code>
和 java
throws
描述符,因为两者都可以向调用者发出信号,作为可以从函数或方法引发的异常类型,尽管两者之间也存在重大差异2:EnsuresOnThrow<>
不仅仅说明可以抛出哪些异常,还规定了保证抛出异常的条件 - 如果以下情况,这在被调用方法中可能是相当繁重的代码:异常情况的识别并不容易。 Javathrows
提供了可以抛出哪些异常的指示(即 IMO,.Net 中的焦点位于用于证明throw
的方法内部,而在 Java 中,焦点转移向调用者确认异常的可能性)。代码合同手册
There are some fleeting similarities between the .Net CodeContract
EnsuresOnThrow<>
and the javathrows
descriptor, in that both can signal to the caller as the type of exception which could be raised from a function or method, although there are also major differences between the 2:EnsuresOnThrow<>
goes beyond just stating which exceptions can be thrown, but also stipulates the conditions under which they are guaranteed to be thrown - this can be quite onerous code in the called method if the exception condition isn't trivial to identify. Javathrows
provides an indication of which exceptions could be thrown (i.e. IMO the focus in .Net is inside the method which contracts to prove thethrow
, whereas in Java the focus shifts to the caller to acknowledge the possibility of the exception).throws
for the same exception on its interface.Code Contracts manual here
对于那些想知道的人,您甚至不需要定义捕获的内容即可将其传递给下一个方法。如果您希望所有错误处理都在一个主线程中,您可以捕获所有内容并将其传递,如下所示:
For those wondering, you do not even need to define what you catch to pass it on to the next method. In case you want all your error handling in one main thread you can just catch everything and pass it on like so:
如果 c# 方法的目的只是抛出异常(就像 js 返回类型所说),我建议只返回该异常。请参阅下面的示例:
如果您想描述异常,可以这样使用摘要:
If the c# method's purpose is to only throw an exception (like js return type says) I would recommend just return that exception. See the example bellow:
If you would like to describe the exception you can use summary as such: