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

  • Home
  • SEARCH
  • 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 6954261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:35:00+00:00 2026-05-27T14:35:00+00:00

I created an exception class like this: public class ServiceException : ApplicationException { public

  • 0

I created an exception class like this:

public class ServiceException : ApplicationException {

    public Dictionary<string, string> Errors { get; set; }

    public ServiceException(Exception ex) : base("Service Exception", ex) {
      Errors = new Dictionary<string, string>();
    }
    public ServiceException() : this(null) {}

}

The following code fails:

protected void log(Exception ex)
{
    if (ex is ServiceException)
    {
        var y = (ServiceException)
        ModelState.Merge(ex.Errors);
    }
    else
    {
        Trace.Write(ex);
        ModelState.AddModelError("", "Database access error: " + ex.Message);
    }
}

It generates the following error on compile:

Error 5 ‘System.Exception’ does not contain a definition for ‘Errors’
and no extension method ‘Errors’ accepting a first argument of type
‘System.Exception’ could be found (are you missing a using directive
or an assembly reference?)

I fixed it by doing the following:

if (ex is ServiceException)
{
    var y = (ServiceException)ex;
    ModelState.Merge(y.Errors);
}

Is there some way that I can avoid this messy code using a variable y and then casting it ? Without declaring the intermediate variable y the code does not pass a syntax check.

  • 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-27T14:35:01+00:00Added an answer on May 27, 2026 at 2:35 pm

    You need to cast because ex is not a ServiceException. The is operator is simply evaluating telling you that it is a ServiceException, but doesn’t perform the cast to the type.

    Think about what type ex is. It’s of type Exception, and generally speaking you don’t know what it is. By calling ex is ServiceException, you’re checking if the dynamic type of ex is ServiceException. But the static type is still Exception. That doesn’t change.

    C# is a statically typed language, so the static type of ex, which is Exception won’t magically change to ServiceException after you use the is operator. You still need to cast it yourself.

    Rather, you may wish to use the as operator:

    protected void log(Exception ex)
    {
        ServiceException se = ex as ServiceException;
        if (se != null)
            ModelState.Merge(se.Errors);
        else
        {
            Trace.Write(ex);
            ModelState.AddModelError("", "Database access error: " + ex.Message);
        }
    }
    

    ex as ServiceException tries to cast ex to a ServiceException. If ex isn’t a ServiceException, the cast fails and it returns null.

    Alternatively, if you’re really against using an extra local variable, then you can just cast it and then use it immediately:

    if (se is ServiceException)
        ModelState.Merge(((ServiceException) se).Errors);
    

    Honestly though, I don’t see why you’d want this though. Doing the check first (using as or is) followed by using the downcasted exception is the best idea here. It’s cleaner to actually use the extra variable because you’re showing your intent and it’s immediately obvious what you want to achieve.

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

Sidebar

Related Questions

I created a file called Hello.java that looks like this: public class Hello {
I created a database in my class like public final String CRAZY_ALARM_DB_NAME = CrazyDb;
I wanted to create my own Python exception class, like this: class MyException(BaseException): def
I've created class that takes Exception type in constructor private readonly Exception _exception; public
i have created synchronized arrayList like this import java.text.SimpleDateFormat; import java.util.*; class HelloThread {
Should I initialize class fields at declaration like this? public class SomeTest extends TestCase
I created a stack class like this. Some times it runs and sometimes it
I have 2 classess. Role and User like this Role Public Class Role Public
How a user exception class is created from standard exception? Addressing below cases Say
I've created a custom module (which currently only defines a new Exception class), and

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.