Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8747825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:25:17+00:00 2026-06-13T12:25:17+00:00

I have setup so that if an Exception is thrown I can display it

  • 0

I have setup so that if an Exception is thrown I can display it with my custom error page. But in some cases I don’t want to be navigated to the error page, but want it to display a simple dialog window.

public ActionResult Page1()
{
    //The custom error page shows the exception, if one was thrown

    throw new Exception( "An exception was thrown" );

    return View();
}


public ActionResult Page2()
{
    //A dialog should show the exception, if one was thrown

    try
    {
        throw new Exception( "An exception was thrown" );
    }
    catch( Exception ex )
    {
        ViewData["exception"] = ex;
    }
    return View();
}

Is it possible to have a CustomAttribute to handle an exception which has been thrown in an Controller action? If I added CatchException to Page2, can I automate the process of storing the exception in the ViewData, each time an exception was thrown. I don’t have much experience of CustomAttributes and I’d be much appreciated if you could help me.

The Page2 example works perfectly fine, I just want to make the code cleaner as it isn’t really pretty to have try catches in every action (where I want to show a dialog).

I am using .NET MVC 4.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:25:18+00:00Added an answer on June 13, 2026 at 12:25 pm

    You can create a base controller that catch the exceptions and handle it for you.
    Also, looks like the Controllers already have a mechanism to do that for you. You’ll have to override the OnException method inside the controller. You can get a good example here:
    Handling exception in ASP.NET MVC

    Also, there’s another answer on how to use the OnException here:
    Using the OnException

    By using that, your code will be cleaner, since you will not be doing a lot of try/catch blocks.

    You’ll have to filter the exception you wanna handle. Like this:

    protected override void OnException(ExceptionContext contextFilter)
    {
        // Here you test if the exception is what you are expecting
        if (contextFilter.Exception is YourExpectedException)
        {
            // Switch to an error view
            ...
        }
        //Also, if you want to handle the exception based on the action called, you can do this:
        string actionName = contextFilter.RouteData.Values["action"];
        //If you also want the controller name (not needed in this case, but adding for knowledge)
        string controllerName = contextFilter.RouteData.Values["controller"];
        string[] actionsToHandle = {"ActionA", "ActionB", "ActionC" };
    
        if (actionsTohandle.Contains(actionName)) 
        {
             //Do your handling.
        }
    
        //Otherwise, let the base OnException method handle it.
        base.OnException(contextFilter);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a setup function that runs onload to add some behaviours to elements.
I have a site setup that is working fine in ie8 and firefox but
So I have a page setup that lets you expend text boxes upon click:
I have a product setup executable that copies some files to the user's hard
I have a custom exception filter that I'm using to catch a custom exception
I am using WPF. I have a static class that performs some setup not
I have a setup.py that needs to support both Python 2 and 3. The
I have a setup project that installs my app to the typical program files
I have an Setup project that installs App.exe. App.exe depends on 1) A third
I have a setup application that if user clicks the same setup twice, they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.