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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:16:11+00:00 2026-05-13T06:16:11+00:00

I’m a moderate fan of using Exception subtypes to help describe the problem that

  • 0

I’m a moderate fan of using Exception subtypes to help describe the problem that caused the exception. For instance, let’s say that I’m writing a method that does not allow int values greater than 100 to be passed in as arguments. I’ll often start the method with a condition to guard against this, and throw an exception if it occurs. I’ll probably throw a System.Argument exception (with an appropriate message, inner exception, and Data) to indicate this.

Right now I have a class which has two properties. For the object to be considered “valid”, exactly one of these properties must have a non-null value, and the other one must be null. I happen to have a defect case where this isn’t true, and I want to throw an exception when this is detected.

My question is: what is the best Exception subclass to use in this case? I don’t see an (Invalid)StateException subclass. ApplicationException is the obvious fallback choice, but it’s very generic and so I’m hoping for something better.

My particular interest at the moment is in .NET Exception subclasses, but since this same problem applies to multiple languages I wouldn’t consider discussions of other language solutions to be too off-topic. For instance: Java has IllegalStateException which seems to describe my needs perfectly. I’ve also favorited a similar question for Ruby.

  • 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-13T06:16:11+00:00Added an answer on May 13, 2026 at 6:16 am

    Simple Answer (on topic):

    If we’re purely talking about API misuse and the design means such situations are unavoidable, InvalidOperationException is probably what you’re looking for. It’s generally used when the caller has done something incorrect, such as calling a particular method on an object when it is not in a valid state to handle the call. E.g. calling “pop” on an empty stack, or calling a “Send” method on a socket that has yet to have its “Connect” method called.

    Not so Simple Answer (slightly off topic):

    It depends. Things that spring to mind:

    • Is your class being misused by
      callers or is it bugged?
    • Can you change the API to avoid the
      situation entirely?
    • Can you add validation
      steps elsewhere to detect the
      condition as it occurs?

    If you can avoid the situation occurring or detect the invalid state sooner/in a different place (e.g. when a value is set), then it may be better to change the design.

    If the class has a bug and the case should never happen, then I’d think twice before adding an exception for it. I really like Eric’s answer to another question regarding such situations.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build

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.