简体   繁体   中英

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.

    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. I am wrapping the appropriate method calls within the OnCreate with a try catch. 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.

I am wondering a few points.

  • Are there better ways of doing this
  • How can i replicate the above Java code in c#
  • 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.

There is a better way of doing this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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