0

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.

2 Answers 2

1

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.

// on your base page
protected void TryAction(Action action)
{
    try
    {
        action();
    }
    catch(Exception e)
    {
        // write exception to output (Response.Write(str))
    }
}

protected void TryFunction<T>(Function<T> func)
{
    try
    {
        return func();
    }
    catch(Exception e)
    {
        // write exception to output (Response.Write(str))

        return default(T);// have to return something now because it's a function.
    }
}

then your data access method could look like

protected void Button1_Click(object sender, EventArgs e)
{
    TryAction(() => {
        // Do stuff here - save to database etc
    });
}

protected void Button1_Click(object sender, EventArgs e)
{
    return TryFunction<string>(() => {
        // Do stuff here - save to database etc
        return "woo";
    });
}

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Is there any way before it bubble up to Page_Error, i can catch it and continue with rest rendering of the page. Something like implementing the HttpModules or Handler?
The snippet I have given you here will catch it inside the button click handler.
I need to include the snippet in all code behind files. Instead something i can do it with adding some method in pagebase by implementing the HttpModules and HttpHandlers?
// 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 protected void Button1_Click(object sender, EventArgs e) { List<T> _lst; TryAction(()={ //Call BLL Method to retrieve the list of BO. _lst = BLLInstance.GetAllList(a,b); }); } It is Showing the Error, Can you please tell me how to use TryAction Method.
Edit your code into your question, then it should be more readable. Then let me know what error you're getting, or what you're trying to achieve.
1

Don't catch the exception and continue. That is hardly ever the right think to do and never display exception information to the user. The technical nature of these errors would anoy them and it leads to information leakage, which is a security problem.

Instead, configure the application to show custom error messages RemoteOnly and use a logging framework to log thrown exceptions. ELMAH is a great framework for this. Get it from NuGet.

2 Comments

The Application is Handled by Technical Person. There are around 20 field to enter the data on the form. He enters 20 fields when he clicks update button it may throw the exception because of unique key constraint, he looks at the exception message then again enter the proper data to the unique field. Please have any suggestion let me know.
Dont throw exceptions for things that the user can misenter. Validate your data. You can use DataAnnotations for instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.