How to use Java-style throws keyword in C#?

前端 未结 10 2182
说谎
说谎 2020-11-27 04:39

In Java, the throws keyword allows for a method to declare that it will not handle an exception on its own, but rather throw it to the calling method.

Is there a sim

10条回答
  •  日久生厌
    2020-11-27 05:16

    Yes this is an old thread, however I frequently find old threads when I am googling answers so I figured I would add something useful that I have found.

    If you are using Visual Studio 2012 there is a built in tool that can be used to allow for an IDE level "throws" equivalent.

    If you use XML Documentation Comments, as mentioned above, then you can use the tag to specify the type of exception thrown by the method or class as well as information on when or why it is thrown.

    example:

        /// This method throws an exception.
        /// A path to a directory that will be zipped.
        /// This exception is thrown if the archive already exists
        public void FooThrowsAnException (string myPath)
        {
            // This will throw an IO exception
            ZipFile.CreateFromDirectory(myPath);
        }
    

提交回复
热议问题