简体   繁体   English

Xamarin C#抛出异常

[英]Throwing Exceptions Xamarin c#

I am wondering if i am able to get a method to throw an exception similarly to the below Java code. 我想知道我是否能够像下面的Java代码一样获得引发异常的方法。

    private void iAmChecked() throws FileNotFoundException {
     FileReader fr = new FileReader("Not Here.txt");
    }

I am currently developing an Android Application using Xamarin, I want the app to display a Dialog message if internet connection is lost. 我目前正在使用Xamarin开发一个Android应用程序,如果互联网连接断开,我希望该应用程序显示对话框消息。 I am wrapping the appropriate method calls within the OnCreate with a try catch. 我用尝试捕获将适当的方法调用包装在OnCreate中。 The Catch will display a Dialog message and then terminate the application. 捕获将显示一个对话框消息,然后终止该应用程序。 I am aiming for the methods to throw an error back to the onCreate method so that I can do this. 我的目标是使错误返回到onCreate方法,以便我可以执行此操作。

I am wondering a few points. 我想知道几点。

  • Are there better ways of doing this 有更好的方法做到这一点
  • How can i replicate the above Java code in c# 我如何在C#中复制上述Java代码
  • Will it work 它会工作吗

Thanks, Joe 谢谢乔

That code doesn't fires any exception by itself, it only indicates it can throw an exception, but not by itself but by the functions called inside it. 该代码本身不会引发任何异常,它仅表示可以引发异常,但不能引发异常,而可以引发内部调用的函数。 On C# that's unnecesary, you don't need to specify what kind of exceptions your code can throw. 在不必要的C#上,您无需指定代码可以引发哪种异常。

There is a better way of doing this: 有一种更好的方法:

bool AmIChecked()
{
    return System.IO.File.Exists("Not Here.txt");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM