Using exception filtering in c# 6.0 (with typeof()) VS. catching custom exception -
i have method manage exception handling below. question that, in case typeof
exception our point, approach recommended? using
catch (system.exception ex) when(ex.gettype() ==typeof(exconeexception)){...}
or
catch (exconeexception ex) {...}
public t mymethod<t>(func<t> codetoexecute) { try { return codetoexecute.invoke(); } catch (system.exception ex) when(ex.gettype() ==typeof(exconeexception) ) { throw new exconeexception(ex.message,ex); } catch (system.exception ex) { throw new exctwoexception(ex.message, ex); } }
update: solution has 3 projects, ui, services , dataaccess. each part has own custom exception-handler class. imagine that, code in question in service project. codes should call method execution. if there run-time error type of exconeexception, means error in service section, else, there should error in data access part; so, exctwoexception should thrown. approach helps me in bubbling error ui level details. didn't know, that, in case can use c# 6 properties, when have filtering on exception type, approach better, using catch-when or mentioning exception type argument of catch?
why ever consider that? mention performance. have measurements make suspicious.
exception filters there filtering exceptions can't catch type, not case.
in code are, also, not re-throwing caught exception. throwing new exception caught exception inner exception when want wrap caught exception more meaningful one.
if intention re-throw, correct code is:
catch (system.exception ex) { throw; }
Comments
Post a Comment