c# - Adding Global Try Catch Exception -
i implemented global try catch mechanism in way. added seperate class called handleexception.cs
public static class handleexception { public static void globaltrycatch(action action, object obj) { try { action.invoke(); } catch(sqlexception ex) { obj.gettype().getproperty("success").setvalue(obj.gettype(), false); obj.gettype().getproperty("failuremessage").setvalue(obj.gettype(), ex); } catch(exception ex) { obj.gettype().getproperty("success").setvalue(obj.gettype(), false); obj.gettype().getproperty("failuremessage").setvalue(obj.gettype(), ex); } } }
and way calling it.
public override result<int> update(userprofile data) { var result = new result<int> { success = false }; handleexception.globaltrycatch(() => { sqlparameter[] sparam = { dbhelper.createparameter("@userid", parameterdirection.input, sqldbtype.int, data.userid), dbhelper.createparameter("@firstname", parameterdirection.input, sqldbtype.varchar,100, data.firstname), dbhelper.createparameter("@lastname", parameterdirection.input, sqldbtype.varchar,100, data.lastname), dbhelper.createparameter("@gender", parameterdirection.input, sqldbtype.char,1, data.gender), dbhelper.createparameter("@dob", parameterdirection.input, sqldbtype.date, data.dateofbirth), dbhelper.createparameter("@imageurl", parameterdirection.input, sqldbtype.varchar, 150, data.imageurl), }; using(var sql = new dbhelper()) { sql.executespreturnscalar("userprofile_update", sparam); } result.success = true; }, result); return result;
}
my questions are
- is standard practice implementing global try catch mechanism or there other standard way implement this?
i had used in
globaltrycatch
method. whether way can assign value property passing generic object?obj.gettype().getproperty("success").setvalue(obj.gettype(), false);
is standard practice implementing global try catch mechanism
no, not. moreover, mentioned "global try-catch mechanism" bad practice. wrapping every method in try-catch
assumes, know, after any exception has been thrown. in real world false. @ sample:
void anymethod() { var result = // ... handleexception.globaltrycatch(() => { /* action 1 */}, result); // should check result continue? // if so, typical error-code approach, annihilates // preferences, provided .net exceptions; // if shouldn't check it, behavior of our code, // if state broken after action 1? handleexception.globaltrycatch(() => { /* action 2 */}, result); // same questions handleexception.globaltrycatch(() => { /* action 3 */}, result); }
similar approach time-to-time being used log exceptions (due absence of out-of-box aspects inmplementation in .net):
void execute(action action) { try { action(); } catch (exception e) { logger.log(e); throw; } } t execute<t>(func<t> func) { try { return func(); } catch (exception e) { logger.log(e); throw; } }
but:
1) logs full exception information (e.g., code missing stack trace , inner exceptions);
2) re-throws same exception (this allows use benefits .net exceptions);
3) wraps limited number of top-level methods, not every method.
whether way can assign value property passing generic object?
you this:
interface iactionresult { bool success { get; set; } string failuremessage { get; set; } } public static void globaltrycatch<t>(action action, t obj) t : iactionresult { // ... }
but doesn't cancel answer on 1st question.
Comments
Post a Comment