1)I have the PageBase class that inherits from Page Class. The all code behind inherit from PageBase class. So I want to add the exception handling logic to PageBase class.
In Page_Error Event I added the logic to handle the exception, but the I want page to be rendered properly with exception details showing on the top of Web Page.
e.g
On web form I have button control that updates the data in Database. On button click i have the logic to update the data. While updating it may throw exception. I dont want to put Try catch block on button click save logic. Instead I want some method or event on PageBase to catch the exception and render the page entirely with all controls and exception in the header.
2)`// in the Base class of CodeBehind page, I have Written
protected void TryAction(Action action)
{
try
{
action();
}
catch(Exception e)
{
// write exception to output (Response.Write(str))
}
}
In CodeBehind I have written the code for Button Click Event
Private List<T> GetData(string a, string b)
{
List<T> _lst;
TryAction(()=>{
//Call BLL Method to retrieve the list of BO.
_lst = BLLInstance.GetAllList(a,b);
});
return _lst;
}
This Works fine, I want,
Private List<T> GetData(string a, string b)
{
TryAction(()=>{
//Call BLL Method to retrieve the list of BO.
return BLLInstance.GetAllList(a,b);
});
}
It is Showing the Error”System.Action retuens void”, Can you please tell me how to use TryAction Method if we have return type.
When the exception is thrown, if you don’t catch it, it will bubble up to Page_Error and then Application_Error. Once it gets to Page_Error tho it’s already broken the page rendering. I don’t think there’s a way to continue rendering the rest of the page. If you’re looking to increase code re-use of your error handling logic you could use an anonymous method.
then your data access method could look like
It is worth noting that if an exception occurs in the function you might not get a useful value back from it and you should code accordingly.