C# 明确定义抛出什么异常 [英] C# explicitly defining what exceptions are thrown

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

问题描述

在 Java 中,您可以使用throws"关键字明确定义抛出哪些异常.这样,任何调用您的方法的人都知道要捕获什么.

In Java, you explicitly define what exceptions are thrown using the "throws" keyword. That way, anyone calling your method knows what to catch.

C# 中有什么东西吗?如果没有,我如何知道要捕获哪些异常,或者如何让其他人知道要捕获哪些异常?

Is there something in C#? If not, how do I know what exceptions to catch, or how do I let others know what exceptions to catch?

另外,如果我正在定义一个接口,有没有办法说methodX() 应该在出错时抛出这个异常"?

Also, if I am defining an interface, is there a way to say "methodX() should throw this exception on error"?

推荐答案

C# 中没有等效的东西:检查异常的问题

There is nothing equivalent in C#: The Trouble with Checked Exceptions

除了文档之外,没有办法声明一个接口来说明methodX() 应该在出错时抛出这个异常".

Other than documentation, there is no way to declare an interface to say "methodX() should throw this exception on error".

这篇关于C# 明确定义抛出什么异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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