c# - A Method which return a value cannot be passed to exception handler -
c# - A Method which return a value cannot be passed to exception handler -
i'm handling exceptions centrally in method (executeaction(action action)
). have problem when passing action method when returns value.
in next code i'm getting error:
since 'system.action' returns void, homecoming keyword must not followed object expression
how solve problem?
public decimal calculateinstalment(decimal amount, int months) { this.executaction(() => { var result = amount / months; homecoming math.round(result, 2); }); } protected bool executaction(action action) { seek { action(); homecoming true; } grab (nullreferenceexception e) { _messageservice.showerrormessage(e); homecoming false; ; } grab (system.data.sqltypes.sqltypeexception e) { _messageservice.showerrormessage(e); homecoming false; } grab (system.data.sqlclient.sqlexception e) { _messageservice.showerrormessage(e); homecoming false; } grab (system.exception e) { _messageservice.showerrormessage(e); homecoming false; }; }
as others have said, action types not homecoming value, can refer outer variables. if you're trying value, consider setup this:
public decimal calculateinstalment(decimal amount, int months) { var result = 0.0; this.executaction(() => { result = math.round((amount / months), 2); }); homecoming result; } protected bool executaction(action action) { seek { action(); homecoming true; } grab (nullreferenceexception e) { _messageservice.showerrormessage(e); homecoming false; ; } grab (system.data.sqltypes.sqltypeexception e) { _messageservice.showerrormessage(e); homecoming false; } grab (system.data.sqlclient.sqlexception e) { _messageservice.showerrormessage(e); homecoming false; } grab (system.exception e) { _messageservice.showerrormessage(e); homecoming false; }; }
c# .net winforms exception-handling mvp
Comments
Post a Comment