ashutosh raina
ashutosh raina

Reputation: 9314

How do i throw an exception explicitly in c#?

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 .

Upvotes: 0

Views: 1812

Answers (2)

Reed Copsey
Reed Copsey

Reputation: 564333

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.

Upvotes: 1

lysergic-acid
lysergic-acid

Reputation: 20050

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

Upvotes: 6

Related Questions