6

I have this code in Java, that I used to report exceptions (throws FileNotFoundException, IOException, ClassNotFoundException).

Example:

private void functionName() throws FileNotFoundException, IOException, ClassNotFoundException{}

I need to do this in C#, how can I do that?

linquize
  • 19,828
  • 10
  • 59
  • 83
S.P
  • 73
  • 5
  • 18
    You can't. C# doesn't support declaring checked exceptions, or any type of exceptions for that matter, in the method signature. Best you can do is add some documentation that declares which exceptions are expected. – sstan Dec 25 '15 at 21:35
  • 6
    Possible duplicate of [What are checked exceptions in Java/C#?](http://stackoverflow.com/questions/9371686/what-are-checked-exceptions-in-java-c) – sstan Dec 25 '15 at 21:37
  • 2
    To add onto what @sstan said, you can use the `/// ...` tag, described [here](https://msdn.microsoft.com/en-us/library/w1htk11d.aspx). This is just documentation, though, and not checked exceptions. – Nate Barbettini Dec 26 '15 at 00:11
  • 3
    Here's an article on why the designers of C# choose not to include checked exceptions into the language: http://web.archive.org/web/20070314071137/http://msdn2.microsoft.com/en-us/vcsharp/aa336812.aspx – Stefan Woehrer Jan 14 '16 at 10:30
  • Possible duplicate of [How to use Java-style throws keyword in C#?](http://stackoverflow.com/questions/3465465/how-to-use-java-style-throws-keyword-in-c) – Aditya Korti Jan 14 '16 at 11:01

1 Answers1

1

It's pretty simple. In C#, you can't directly use a throws statement, because there isn´t. You may want to use this code:

    private void functionName(){ throw new IOException();}

This throws an IOException. As IOException being a class, you need to create a new one, with new statement.

TheCrimulo
  • 433
  • 1
  • 4
  • 14