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 3306596
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:18:26+00:00 2026-05-17T21:18:26+00:00

I have a custom exception class: public class MyException: Exception { public MyException(MyExceptionEnum myError)

  • 0

I have a custom exception class:

public class MyException: Exception
{
   public MyException(MyExceptionEnum myError) : base(myError.ToDescription()) { }
   public MyException(MyExceptionEnum myError, Exception innerException) : base(myError.ToDescription(), innerException) { }
}

.ToDescription is a extension method on MyExceptionEnum to provide enum-to-string mapping for the exception error details.

Here’s how i throw it:

if (someCondition)
   throw new MyException(MyExceptionEnum.SomeError);

So i use my first ctor, which creates a new Exception with a given message.

Now onto the Controller:

[HttpPost]
public ActionResult UpdateFoo(Foo model)
{
   try
   {
      _fooService.UpdateModel(model);
      _unitOfWork.Commit();
   }
   catch(MyException myException)
   {
      ViewData.ModelState.AddModelError("ModelErrors", myException);
   }

   return View("Index", model);
}

And finally a snippet from the View:

<%: Html.ValidationMessage("ModelErrors") %>

Doesn’t work (exception is thrown when i debug, error is added to model state, but nothing shown on page).

But if i change to the following line:

ViewData.ModelState.AddModelError("ModelErrors", myException.Message);

It works.

AddModelError has two overloads:

  1. string, Exception (doesn’t work for me)
  2. string, string (works)

What is the use of the first overload then? My Exception does have an inner exception message, so i would have thought the HTML Extension would render that?

How do we handle custom exceptions with the ModelState then? Is using the second overload correct?

  • 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-05-17T21:18:27+00:00Added an answer on May 17, 2026 at 9:18 pm

    Doesn’t matter whether it’s a custom exception or pre-defined one. It just doesn’t work.
    If you have a chance to look at the MVC source code for ModelError class, you can see that it has an public string property ErrorMessage which is used to display the error when validation happens (in ValidationExtensions class).

    In the overload constructor of ModelError(Exception exception), though, it just sets the ErrorMessage property as empty string, rather than exception.Message. That’s why you see nothing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created custom exception class public class Web2PDFException : Exception { public Web2PDFException(string
I have a custom sharepoint web part that is throwing the following exception: System.Security.SecurityException:
I have a custom exception filter that I'm using to catch a custom exception
This is my custom collection declaration. public interface IMenuCollection<T> : ICollection<T> public class HTMLMenuCollection:
I have been in both situations: Creating too many custom Exceptions Using too many
I have custom coded several enterprise applications for mid to large organizations to use
I have custom errors configured in my web.config, but IIS 6.0 is returning the
Can you have custom client-side javascript Validation for standard ASP.NET Web Form Validators? For
I have a few models that need to have custom find conditions placed on
hi i would like to create a custom calendar, this calendar will have custom

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.