以下を考えると:
[HttpGet]
[ActionName("GetContent")]
public HttpResponseMessage GetContent(int id)
{
Content content = _uow.Contents.GetById(id);
if (content == null)
{
var message = string.Format("Content with id = {0} not found", id);
return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
}
else
{
return Request.CreateResponse(HttpStatusCode.OK, content);
}
}
と:
[HttpGet]
[ActionName("GetContent")]
public HttpResponseMessage GetContent(int id)
{
try
{
Content content = _uow.Contents.GetById(id);
if (content == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return Request.CreateResponse<Content>(HttpStatusCode.OK, content);
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}
私は 2 つのコーディング スタイルを見てきました。1 つは例外を使用し、もう 1 つは使用しません。1 つは CreateResponse<> を使用し、もう 1 つは CreateResponse() を使用します。誰かがこれらを使用することの利点/欠点を教えてもらえますか? 私が見る限り、2 番目の方法はより完全に見えますが、このような単純なことに本当に try / catch を使用する必要があるのでしょうか?