asp.net mvc - Is it a bad practice to have separate try/catch blocks inside the same Action method -


i have following delete action method, perform 2 separate tasks:-

  1. delete record third party application using api call.
  2. delete record database on our own system using entity framework.

my action method looks follow:-

[httppost, actionname("delete")] public actionresult deleteconfirmed(int id) {     var message = "";     var status = "";     var tag = "";     resource resource = new resource();     try     {         rack rack = repository.findrack(id);         tag = rack.technology.tag;          resource = repository.getresource(rack.technology.it360id.value);     }     catch (nullreferenceexception)     {         return json(new         {             issuccess = "false"         }, jsonrequestbehavior.allowget);     }     catch (dbupdateexception)     {         return json(new         {             issuccess = "alreadyused"         }, jsonrequestbehavior.allowget);     }      using(var client = new webclient())     {         var query = httputility.parsequerystring(string.empty);          query["username"] = "testuser";         query["assettype"] = resource.componentdefinition.componenttype.componenttypename;         query["operation"] = "deleteasset";         query["assetname"] = resource.resourcename;         var url = new uribuilder("http://win-spdev:8400/servlets/assetservlet");         url.query = query.tostring();         try         {             string xml = client.downloadstring(url.tostring());             xmldocument doc = new xmldocument();             doc.loadxml(xml);             status = doc.selectsinglenode("/operation/operationstatus").innertext;             message = doc.selectsinglenode("/operation/message").innertext;         }         catch (webexception ex)         {}     }      if (status.toupper() == "success")     {         try         {             repository.deleterack(id, user.identity.name);             repository.save();              return json(new             {                 issuccess = "true", id = id, description = tag             }, jsonrequestbehavior.allowget);         }         catch (nullreferenceexception)         {             return json(new             {                 issuccess = "false"             }, jsonrequestbehavior.allowget);         }         catch (dbupdateexception)         {             return json(new             {                 issuccess = "alreadyused"             }, jsonrequestbehavior.allowget);         }     }      return redirecttoaction("delete", new     {         id = id     }); } 

as since using entity framework perform deletion , api call, ended separate try/catch blocks . action method logic consider poor design since having multiple try/catch blocks inside same action method? , better approach can follow?

separate error cases not bad pracice.

what bad practice, though, catching unspecific errors. returning "alreadyused" all dbupdateexceptions. there might other causes 1 planned for. if happens swallow error , have silent bug. might lack imagination right cases might because never know bugs before happen. advise catch more specific either interpreting exception object (maybe interpret message, god forbid) or tightening region catch covers statement can give error.

in short, don't swallow exceptions indicating bugs. bugs happen time, want know them , fix them.

also, same reason, never ever catch nullreferenceexception. bugs convention. insert if deal null.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -