0

In Java i can explicitly specify that a method throws an exception like:

public void read() throws IOException{}

What is the c# equivalent of this?

What are the best practices for throwing custom exceptions?

I have made them serializable and provided a streaming context also .

Chris Ballance
  • 33,810
  • 26
  • 104
  • 151
ashutosh raina
  • 9,228
  • 12
  • 44
  • 80

2 Answers2

6

What you're referring to is the actual documentation of code that throws exceptions.

Read this thread that discusses this question: How to document thrown exceptions

Community
  • 1
  • 1
lysergic-acid
  • 19,570
  • 21
  • 109
  • 218
  • 1
    @ashutosh What do you want then? in C# you can only document what exceptions a method throws. You can't make them a part of the signature of the method. Ie there's no way of having the compiler tell the caller of a method that he didn't handle specific exceptions. He can read the documentatio if such exist or figure it out at run time – Rune FS Sep 03 '11 at 17:46
  • @Rune FS ,@liortal yes ,i had a closer look at the exception specification for C#,it seems i just have to specify in the documentation what exceptions my method might throw and have the caller handle it . thanks for your help. – ashutosh raina Sep 03 '11 at 17:48
1

The topic in MSDN called Exceptions and Exception Handling covers Creating and Throwing Exceptions in detail.

However, exception specifications are not part of the C# language like they are in Java. You must rely on documentation to tell the user of your API what exceptions may be thrown. You cannot force a user to handle an exception, or document it in code.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373