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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:48:35+00:00 2026-06-01T12:48:35+00:00

This is probably a basic question, better explained through code: public void checkStatus {

  • 0

This is probably a basic question, better explained through code:

    public void checkStatus {
        int status = UNKNOWN;
        if (somecondition) {
             status = STATUS_UP;
        } elseif (someothercondition) {
             status = STATUS_DOWN;
        }
    }

So the problem is, by definition I do not know all the possible conditions that might affect the STATUS, and I didn’t want the compiler to throw ‘my not be defined’ error by not initializing the status local variable.

Bottom line my app won’t work with a status set to UNKNOWN, I’ve just set it to shut up the compiler.

Question: how can I approach this elegantly, I’ve considered throwing an fatal exception at the end of the method should the status still be set to UNKNOWN, but that feels a bit ‘ugly’.

Thank you.

  • 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-06-01T12:48:36+00:00Added an answer on June 1, 2026 at 12:48 pm

    I always put a throw at the end of the if / then / else chain, and do not assign the default value to the variable upfront. If the exception is thrown, it tells me not only that the application cannot continue, but also reports the reason why it cannot proceed. Moreover, it does that as soon as it finds out that it cannot proceed, which is very desirable for error reporting (generally, you want to report an error as soon as possible).

    There is one special case, when you structured your code in such a way that the combination of someconditions is such that you never run off the tail of the if / then / else chain, but the compiler cannot verify it automatically. For example, you validated the input before, and you know that there is no path through your code that can change it. In this specific case you should use an assertion rather than throwing an exception, because it indicates an error with your reasoning about your code, rather than a simple coding error.

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

Sidebar

Related Questions

This is probably a pretty basic question, but just something that I wanted to
This is probably a basic html/css question... I have a simple one-button form that
Problem : This is a probably a very basic question, but how do I
This is probably a basic LINQ question. I have need to select one object
This is probably a basic question. I have two projects: A web application project,
This probably is a very very basic question but i can't seem to find
This is probably a very basic question and I know how to do it
This is probably a very basic question but I haven't found any detailed information
This is probably a basic question, since I'm new to WPF.. I have a
Sorry for the probably basic question, but I'm trying to understand some code and

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.