Yesterday someone guided me through this article and I'm wondering whether I should use try-catch implementation or not? I have the following code below which is actually a RESTful service and I have implemented the try-catch pattern. I'm throwing errors only in debug mode just for testing purpose and during the release mode, I'm catching errors and returning the meaningful error messages such as Http Status Codes.
After reading this article, I'm confused whether I'm on the right track and if not, do I need to remove try-catch and throw errors instead of such meaningful messages to the end user?
Any code review/feedback is appreciated.
namespace NewEmployeeBuddy.Facade.Controllers.Employee
{
[RoutePrefix("api/employee")]
[RequireHttps]
[BasicAuthorization]
public class EmployeeController : ApiController
{
#region Properties
private IEmployeeService _employeeService { get; set; }
#endregion
#region Constructor
public EmployeeController()
{
//_employeeService = new EmployeeService();
}
public EmployeeController(IEmployeeService employeeService)
{
_employeeService = employeeService;
}
#endregion
#region Actions
[HttpPost, Route("add")]
[ValidateRequest]
public IHttpActionResult AddEmployee(AddEmployeeRequestDTO request)
{
try
{
if (request == null)
return Content(HttpStatusCode.BadRequest, ErrorCodes.E002);
var response = _employeeService.AddEmployee(request);
if (response.OperationResult)
return Content(HttpStatusCode.Created, response);
return Content(HttpStatusCode.InternalServerError, ErrorCodes.E001);
}
catch (Exception ex)
{
#if (DEBUG)
Logger.Instance.Error(ex);
throw ex;
#endif
//Logger.Instance.Error(ex);
//return Content(HttpStatusCode.InternalServerError, ErrorCodes.E001);
}
}
#endregion
}
}
//No need to create the concrete instance. Autofac takes care of it.
- this is not true if you use this constructor. \$\endgroup\$