# Exception, errors handling, best practice in WebApi Core

I'm making my WebApi project.

My logic is, the controller didn't see data. It only triggers a service that returns objects to it. My simple method from TaskService looks like this:

{

{
{
}

this.context.SaveChanges();

}

return null;
}

As you can see, in this method I try to find Task and attach Foreign Key to User by his UserId. If Task is already taken by another user, I throw a custom exception.

In my controller, Action looks like this:

[Authorize(Roles = "Developer, Manager")]
{
try
{
{
}

return this.NotFound();
}
{
return this.ValidationProblem();
}
}

Is using a try-catch block in a controller good practice? Is using any exceptions in WebApi good, or I should use the method posted in this article (I like Level 1, seems easy and fun)?

How do I deal with errors that I know that can appear when someone sends a request?

Exceptions are pretty costly so as a general rule of thumb they should be avoided. Especially when we talk about REST endpoint where response time is crucial.

Also, to my taste you're messing up command-query separation principle in your code. I would have rewritten it roughly like this

[Authorize(Roles = "Developer, Manager")]
{
return this.NotFound();
$$`$$