如何知道某个方法是否可以抛出异常 [英] How to know if some method can throw an exception

查看:38
本文介绍了如何知道某个方法是否可以抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Windows 8 和 C# 开发的新手,但我有一定的 Java 编程经验.

I'm new in a developement for Windows 8 and C#, but I have certain experience with Java Programming.

因此,当我尝试在 Java 中创建一些 Json 解析器(例如)时,如果不使用 try - catch 块,我就无法做到这一点,这样我就可以处理异常,但是当我尝试执行在 c# (Windows 8) 中相同,我不使用 try - catch 块,它也可以工作,如下所示:

So, when I try to make some Json parser (for example) in java, I can't do it without use a try - catch block, and this way I can handle the exception, but when I try to do the same in c# (Windows 8) and I don't use the try - catch block it works too, like this:

if (json != null)
{
        JObject jObject = JObject.Parse(json);

        JArray jArrayUsers = (JArray)jObject["users"];

        foreach (JObject obj in jArrayUsers)
        {
            ListViewMainViewModel user = new ListViewMainViewModel(
                (String)obj["email"],
                (String)obj["token"],
                (String)obj["institution"],
                (String)obj["uuidInstitution"]);

            usersList.Add(user);
        }
        return usersList;
    }

}

据我所知,正确的方法是捕获 JsonReaderException,但 Visual Studio 从未就此警告过我.我想知道是否有一种简单的方法可以知道某些方法是否抛出异常,例如在使用 Eclipse 的 Java 上(这是强制实现的 try-catch 块或代码无法编译)

As I know the right way is to catch JsonReaderException, but Visual Studio never warned me on that. I would like to know if there's a easy way to know if some method throw an exception, like is on java using eclipse (it's mandatory implement try-catch block or code wont compile)

推荐答案

您必须为此查阅文档.C# 缺少 throws 关键字.

You will have to consult the documentation for that. C# lacks a throws keyword.

您要查找的术语是检查异常,更多信息可以在 C# 常见问题.

The term for what you are looking for is checked exceptions, and more info can be found in the C# FAQ.

这篇关于如何知道某个方法是否可以抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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