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 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

Ask A Question

Stats

  • Questions 251k
  • Answers 251k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The code have no syntax errors, but as you use… May 13, 2026 at 9:31 am
  • Editorial Team
    Editorial Team added an answer This is probably because server returns error code (500) and… May 13, 2026 at 9:31 am
  • Editorial Team
    Editorial Team added an answer This function will only check of a file exists --… May 13, 2026 at 9:31 am

Related Questions

In order to apply a triggered animation to all ToolTip s in my app,
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.