Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed to wrap functions in a standard try/catch function in c#
    text
    copied!<p>I know there are lots of delegate/func examples but I can't find any examples that will work for me, or I just don't understand them.</p> <p>I'm using asp.net MVC for a website, and the website needs some web service calls for an outside application to interact with my app. These all need a function to execute (going to db and whatnot), and return a similar data model every time. I want to wrap each call in a try/catch and populate the model.</p> <p>Here is the generic code that happens every call.</p> <pre><code>var model = new ResponseDataModel(); try { //execute different code here } catch (Exception ex) { model.Error = true; model.Message = ex.ToString(); } return View(model); // will return JSON or XML depending on what the caller specifies </code></pre> <p>This is one of the controller methods/ functions that I am using</p> <pre><code>public ActionResult MillRequestCoil() { var model = new ResponseDataModel(); try { /* edit */ //specific code string coilId = "CC12345"; //additional code model.Data = dataRepository.doSomethingToCoil(coilId); //replaced code //model.Data = new { Coil = coilId, M3 = "m3 message", M5 = "m5 message" }; model.Message = string.Format("Coil {0} sent successfully", coilId); } catch (Exception ex) { model.Error = true; model.Message = ex.ToString(); } return View(model); } </code></pre> <p>I would like to be able to somehow convert the specific function to a variable to pass into the generic code. I've looked at delegates and anonymous funcs but it's pretty confusing until you do it yourself.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload