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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:29:27+00:00 2026-05-14T04:29:27+00:00

I don’t think this is currently possible or if it’s even a good idea,

  • 0

I don’t think this is currently possible or if it’s even a good idea, but it’s something I was thinking about just now. I use MSTest for unit testing my C# project. In one of my tests, I do the following:

MyClass instance;

try
{
    instance = getValue();
}
catch (MyException ex)
{
    Assert.Fail("Caught MyException");
}

instance.doStuff(); // Use of unassigned local variable 'instance'

To make this code compile, I have to assign a value to instance either at its declaration or in the catch block. I could alternatively return after the Assert.Fail but that’s still a workaround instead of the compiler just knowing that execution cannot continue after this point. Assert.Fail will never, to the best of my knowledge, allow execution to proceed past it, hence instance will never be used without a value. Why is it then that I must assign a value to it? If I change the Assert.Fail to something like throw ex, the code compiles fine, I assume because it knows that exception will disallow execution to proceed to a point where instance would be used uninitialized.

Contrariwise, what if I didn’t want the test to fail, but rather be marked as inconclusive? I could do an Assert.Inconclusive instead of Fail, and it would be nice if the compiler knew execution would not continue after that.

So is it a case of runtime versus compile-time knowledge about where execution will be allowed to proceed? Would it ever be reasonable for C# to have some way of saying that a member, in this case Assert.Fail, will never allow execution after it returns? Maybe that could be in the form of a method attribute. Would this be useful or an unnecessary complexity for the compiler?

Outside Unit Tests

Since people are [validly] pointing out that this is a silly way to write a unit test, consider my question outside the realm of unit testing:

MyClass instance;

if (badThings)
{
    someMethodThatWillNeverReturn();
}
else
{
    instance = new MyClass();
}

instance.doStuff();

Here potentially I could replace the call to someMethodThatWillNeverReturn with throwing an exception, and perhaps if I had stuff to do, I could do it in the constructor for the exception.

Resharper Knows

If I add a return after Assert.Fail or Assert.Inconclusive, Resharper colors return gray and has a tooltip saying “Code is heuristically unreachable.”

  • 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-14T04:29:27+00:00Added an answer on May 14, 2026 at 4:29 am

    Yes, it would be reasonable to have something which indicated that a member would never complete normally – i.e. asserting that the point after the member was unreachable. (This could either be due to an exception or due to looping forever.)

    You’d want there to be something (whether in the CLR or the compiler) to come up with a backup plan for if you’re wrong: what would happen if someone changed Assert.Fail to return normally? You’d potentially want part of code verification to be something that checked it would never return normally.

    I believe there’s a blog post about this idea from someone in Microsoft… I’ll see if I can find it.

    In terms of syntax for representing it, while an attribute is an obvious idea, I quite like the idea of a return type of “never”. Obviously that would potentially clash with existing “never” types, but hey…

    In terms of its usefulness: the obvious workaround is to throw an exception immediately after the statement, but it’s certainly annoying to have to do that. (It’s generally better than a return as it means if the method you’re writing this in has a return type, you don’t have to specify a pointless return value – and you also don’t need to make sure that all out parameters are assigned values.) So it’s not required – but I think it would be nice. Whether it’s the most important thing the C# team can do with their limited budget is a different matter – just to pre-empt Eric 😉

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

Sidebar

Related Questions

Don't need to do this right now but thinking about the future... What would
I don't know if anyone has seen this issue before but I'm just stumped.
I don't think this should be in my view, but instead handled by the
I don't understand where the extra bits are coming from in this article about
I don't expect a straightforward silver bullet answer to this, but what are the
I don't know if this is possible. I display a table of results and
I don't want PHP errors to display /html, but I want them to display
I don’t think I’ve grokked currying yet. I understand what it does, and how
I don't remember whether I was dreaming or not but I seem to recall
I don't currently use ajax.net though I would be open to it if it

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.