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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:26:32+00:00 2026-05-31T03:26:32+00:00

For example, switch (number) { case 1: return DoOne(); case 2: return DoTwo(); case

  • 0

For example,

switch (number)
{
    case 1: return DoOne();
    case 2: return DoTwo();
    case 3: return DoThree();
    case 4: return DoFour();

    default:
        throw new ???Exception("Unexpected number encountered.");
}

For the sake of this question, please assume:

  • number is a private field in a class

  • It is a class invariant that number should always be between 1 and 4; everything else indicates a bug in this class. In other words, if the exception triggers, it is never the caller’s fault but always the class’s author’s.

What is the correct exception to throw in this case?

  • 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-31T03:26:34+00:00Added an answer on May 31, 2026 at 3:26 am

    Edited

    There is no good default CLR exception that fits this situation. You should roll your own. I’d derive directly from exception, as you probably don’t want this error to get mixed up with other types of errors.

    There are situations where a program can recover from this seemingly unrecoverable situation: If you can be sure that the problem in this class can not spread to elsewhere. This is for instance when you use this class as part of a function that has no side effects. This may seem a contrived example, but it isn’t when you purposely write or package code such that it is free from side effects.

    I don’t think you should do something drastic like exiting the program (as suggested in another now deleted answer). As a class author that is not your responsibility.

    As an author of the sofware using this class, I would like to get the opportunity to tell the user that something bad happend, try to save backups, log information, write stuff to databases…. you get the picture.

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

Sidebar

Related Questions

When I try the last example from perlfaq5: How-do-I-count-the-number-of-lines-in-a-file? I get an error-message. What
For my project I am using enums, and I need to implement the switch-case
In case 5 of the switch statement below I'd like the user to input
Is there a way to store user inputs in switch case from one operation
Example code: <html> <head> <script src=jquery-1.3.2.min.js type=text/javascript></script> <script src=jquery-ui-1.7.1.custom.min.js type=text/javascript></script> </head> <body> <table border=1
Example 1: <head> <meta http-equiv=description content=<%= Foo %>/> </head> Renders <meta http-equiv=description content=Bar/> Example
Example: I have an class that inherits from UIImageView. An object creates an instance
Example: I have a selector like this, which I give to another method as
Example Schema: RowID Quantity ModifiedPrice GroupPrice CustomPrice SalePrice ---------------------------------------------------------------------------- 1 5 20.00 0 15.00
Example I have a repository class (DAL): public class MyRepository : IMyRepository { public

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.