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?

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. Commented Dec 25, 2015 at 21:35
  • 6
    Possible duplicate of What are checked exceptions in Java/C#? Commented Dec 25, 2015 at 21:37
  • 2
    To add onto what @sstan said, you can use the /// <exception cref="FileNotFoundException">...</exception> tag, described here. This is just documentation, though, and not checked exceptions. Commented Dec 26, 2015 at 0:11
  • 3
    Here's an article on why the designers of C# choose not to include checked exceptions into the language: web.archive.org/web/20070314071137/http://msdn2.microsoft.com/… Commented Jan 14, 2016 at 10:30
  • Possible duplicate of How to use Java-style throws keyword in C#? Commented Jan 14, 2016 at 11:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.