0

In my app I am trying to use Exception handling. What I am trying to do is to create a separate class and extend it from HttpResponseException.

This is the code I have written until now,

public class ApiExceptions : HttpResponseException
{
    public ApiExceptions(string reason, HttpStatusCode code)
    {
        var response = new HttpResponseMessage
        {
            StatusCode = code,
            ReasonPhrase = reason,
            Content = new StringContent(reason)
        };
        throw new HttpResponseException(response);
    }
}

But I am getting this error,

'System.Web.Http.HttpResponseException' does not contain a constructor that takes 0 arguments

I am really new at exception handling in c#. I would really appreciate the help in this regard.

3 Answers 3

3

the following code should works

: base(...)

If you omit the call to a base constructor it will call the default base constructor automatically and HttpResponseException doesn't have without arguments.

 public class ApiExceptions : HttpResponseException
        {
            public ApiExceptions(string reason, HttpStatusCode code):base(code)
            {
                var response = new HttpResponseMessage
                {
                    StatusCode = code,
                    ReasonPhrase = reason,
                    Content = new StringContent(reason)
                };
                throw new HttpResponseException(response);
            }
        }

anyway i'm not sure why you need to extend HttpResponseException

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

1 Comment

This is a great solution. Anyone using this in production should add a check that the reason string is not greater than 512 characters in length. stackoverflow.com/a/4877267/3761486
3

The HttpResponseException class has two constructors defined, both of which require a value:

HttpResponseException(HttpResponseMessage)
HttpResponseException(HttpStatusCode)

Because your ApiExceptions class inherits from HttpResponseException, it has to provide a compatible constructor. As all the class does is return the HttpResponseException, it doesn't seem necessary for it to inherit from that class at all.

3 Comments

can you provide an example on how to extend that class?
Why do you need to extend the class?
now that I am thinking it feels like I don't need it.
0

If you want to derived form HttpResponseException you should use a code similar to this: In the example is a customize exception that return BadRequest type exception and a custom message.

 public class HttpResponseBadRequestException: HttpResponseException
{
    /// <summary>
    /// Basic
    /// </summary>
    public HttpResponseBadRequestException()
        : this(string.Empty)
    {
    }

    /// <summary>
    /// Specific Constructor. Use this to send a Bad request exception
    /// with a custom message
    /// </summary>
    /// <param name="message">The message to be send in the response</param>
    public HttpResponseBadRequestException(string message)
        : base(new HttpResponseMessage(HttpStatusCode.BadRequest) {
            Content = new StringContent(message) })
    {
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.