How to handle exceptions in a multi-layered ASP.NET Web Application -
i have asp.net web forms application ui, service layer , repository layer.
some of methods in service layer communicates web service, therefore wrap calls web methods in try-catch-finally construct.
suppose have following methods in service layer:
public registrationdetails getregistrationdetails(int userid) public bool registeruser(userdata myuserdata) where registrationdetails , myuserdata object types (classes).
my concern following: if create try-catch-finally wrap call web service within implementation of methods listed above, in case there exception how can return message string if return types registrationdetails , bool?
i thinking adding property every return object not know if solution. instance instead of using bool:
public class registerresponse { public bool isregistered { get; set; } public string exceptionmessage { get; set; } } public registerresponse registeruser(userdata myuserdata) and check if exceptionmessage null or string.empty. approach? thanks
1) mentioned irishchieftain, bubbling exception down forms good, able respond exception better
2) can have reference parameter array stores exception messages generated method
public bool registeruser(userdata myuserdata, optional ref arraylist<string> errors) { if(error) errors.add("this error occured") } 3) instance object, have instance variable of arraylist errors , have returned in property
public class myclass { private arraylist<string> errors = new arraylist<string> public arraylist<string> exceptionmessages() { { return errors; } } public registrationdetails getregistrationdetails(int userid) { } } //used this: myclass c = new myclass(); c.getregistrationdetails(); if(c.exceptionmessages > 0) { //output errors } but prefer first approach - flexibility output formatting
Comments
Post a Comment