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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:26:00+00:00 2026-05-16T10:26:00+00:00

Following method shall only be called if it has been verified that there are

  • 0

Following method shall only be called if it has been verified that there are invalid digits (by calling another method). How can I test-cover the throw-line in the following snippet? I know that one way could be to merge together the VerifyThereAreInvalidiDigits and this method. I’m looking for any other ideas.

public int FirstInvalidDigitPosition {
    get {
        for (int index = 0; index < this.positions.Count; ++index) {
            if (!this.positions[index].Valid) return index;
        }
        throw new InvalidOperationException("Attempt to get invalid digit position whene there are no invalid digits.");
    }
}

I also would not want to write a unit test that exercises code that should never be executed.

  • 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-16T10:26:01+00:00Added an answer on May 16, 2026 at 10:26 am

    If the “throw” statement in question is truly unreachable under any possible scenario then it should be deleted and replaced with:

    Debug.Fail("This should be unreachable; please find and fix the bug that caused this to be reached.");
    

    If the code is reachable then write a unit test that tests that scenario. Error-reporting scenarios for public-accessible methods are perfectly valid scenarios. You have to handle all inputs correctly, even bad inputs. If the correct thing to do is to throw an exception then test that you are throwing an exception.

    UPDATE: according to the comments, it is in fact impossible for the error to be hit and therefore the code is unreachable. But now the Debug.Fail is not reachable either, and it doesn’t compile because the compiler notes that a method that returns a value has a reachable end point.

    The first problem should not actually be a problem; surely the code coverage tool ought to be configurable to ignore unreachable debug-only code. But both problem can be solved by rewriting the loop:

    public int FirstInvalidDigitPosition 
    { 
        get 
        { 
            int index = 0;
            while(true) 
            {
                Debug.Assert(index < this.positions.Length, "Attempt to get invalid digit position but there are no invalid digits!");
                if (!this.positions[index].Valid) return index; 
                index++;
            } 
        }
    }
    

    An alternative approach would be to reorganize the code so that you don’t have the problem in the first place:

    public int? FirstInvalidDigitPosition { 
        get { 
            for (int index = 0; index < this.positions.Count; ++index) { 
                if (!this.positions[index].Valid) return index; 
            } 
            return null;
        } 
    } 
    

    and now you don’t need to restrict the callers to call AreThereInvalidDigits first; just make it legal to call this method any time. That seems like the safer thing to do. Methods that blow up when you don’t do some expensive check to verify that they are safe to call are fragile, dangerous methods.

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

Sidebar

Related Questions

The following method does not work because the inner block declares a variable of
The following method does not compile. Visual Studio warns An out parameter may not
Say we have the following method: private MyObject foo = new MyObject(); // and
Why does the following method hang? public void pipe(Reader in, Writer out) { CharBuffer
I'm using the following method to send mail from Python using SMTP. Is it
Consider the following method signatures: public fooMethod (Foo[] foos) { /*...*/ } and public
I am using the following method: set_browser_log_level 'off' but my log level continues to
I am using the following method to browse for a file: OpenFileDialog.ShowDialog() PictureNameTextEdit.Text =
I have the following method in my code: private bool GenerateZipFile(List<FileInfo> filesToArchive, DateTime archiveDate)
I am using the following method for reading Csv file content: /// <summary> ///

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.