---
title: "How to catch all exceptions in Web API 2?"  
description: "How to catch all exceptions in Web API 2?"  
author: "Anonymous User"  
published: 2015-01-14  
updated: 2015-01-14  
canonical: https://www.mindstick.com/forum/12857/how-to-catch-all-exceptions-in-web-api-2  
category: "asp.net"  
tags: ["c#", "exception handling"]  
reading_time: 1 minute  

---

# How to catch all exceptions in Web API 2?

I'm [writing](https://www.mindstick.com/articles/12997/5-essential-tips-on-resume-writing-for-business-analysts) a [RESTful API](https://www.mindstick.com/articles/335974/retrieve-data-from-restful-api-and-add-paging-in-knockout-js) in [Web API](https://www.mindstick.com/articles/324352/how-to-create-web-api-in-dot-net-core-3-1-mvc) and I'm not sure how to [handle errors](https://www.mindstick.com/forum/157886/how-do-you-handle-errors-and-exceptions-in-angularjs-applications) effectively. I want the API to return JSON, and it needs to consist of the exact same format every single time - even on errors. Here are a couple of examples of what a successful and a [failed](https://www.mindstick.com/forum/160484/error-failed-to-launch-debug-adapter-additional-information-may-be-available-in-the-output-window) [response](https://www.mindstick.com/forum/12719/how-to-make-response-write-display-special-character-like-lt-gt) might look like.

```
Success:{    Status: 0,    Message: "Success",    Data: {...}}Error:{    Status: 1,    Message: "An
error occurred!",    Data: null}
```

If there is an [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) - *any* exception at all, I want to return a response that is formed like the [second](https://answers.mindstick.com/qa/41761/how-did-the-second-industrial-revolution-influence-women-s-roles-in-society) one. What is the foolproof way to do this, so that no [exceptions](https://www.mindstick.com/interview/22871/define-predifined-generic-exceptions) are left unhandled?

## Replies

### Reply by Manoj Bhatt

Implement IExceptionHandler.

Something like:

```
public class APIErrorHandler : IExceptionHandler{    public Task HandleAsync(ExceptionHandlerContext
context, CancellationToken cancellationToken)    {        var jsonResult= Json.Encode(new                                 {                                    Message = new{ Message = context.Exception.Message },                                     Status = ...
// whatever,                                    Data = ... //
whatever                                });         context.Result= new ResponseMessageResult(new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError,
Content = new StringContent(jsonResult) });         return Task.FromResult(0);    }}
```

and in the configuration section of WebAPI (public static void Register(HttpConfiguration config)) write:

config.Services.Replace(typeof(IExceptionHandler), new APIErrorHandler());


---

Original Source: https://www.mindstick.com/forum/12857/how-to-catch-all-exceptions-in-web-api-2

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
