C#检查异常 [英] C# Checked exceptions

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

问题描述

我真正喜欢用Java而不是C#的一项功能是 已检查的异常 .有什么方法可以模拟(也许通过stylecop吗?)或在Visual Studio中打开已检查的异常?

One feature I really liked in Java that isn't in C# is checked exceptions. Is there any way to simulate (maybe via stylecop?) or turn on checked exceptions in Visual Studio?

是的,我知道很多人不喜欢他们,但我发现他们会有所帮助.

Yes I know a lot of people dislike them, but I find they can be helpful.

推荐答案

据我所知,没有办法在C#中执行检查异常.该语言不支持该功能(或错误,具体取决于您对它的看法:).

Far as I know, there's no way to do checked exceptions in C#. That feature (or a bug, depending on how you look at it :)) is not supported by the language.

您最好的选择是将XML注释添加到您的方法中,包括该方法引发的异常,并希望任何调用您代码的人都能阅读文档.

Your best bet would be to add XML comments to your method, include the exceptions thrown by it and hope whoever calls your code reads the documentation.

类似这样的东西:

/// <summary>
/// This is my method that does stuff.
/// </summary>
/// <exception cref="InvalidOperationException">This stuff can't be done!</exception>
public void DoStuff() 
{
    // do stuff
}

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

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