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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:35:07+00:00 2026-05-27T21:35:07+00:00

I got the question: What do you prefer, exception handling or if-condition? for an

  • 0

I got the question:

“What do you prefer, exception handling or if-condition?”

for an interview. My answer was that exception handlers are preferred only for exceptional circumstances like a disk permission error on file write. The interviewer seemed to be expecting some other answer. What is the correct answer?

EDIT: Any particular example where exception handling is commonly used when an if-condition would have been more appropriate?

  • 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-27T21:35:07+00:00Added an answer on May 27, 2026 at 9:35 pm

    As this question is tagged “C#”, we can refer to the .NET Framework Design Guidelines as a good starting point for answering these types of questions. This is the guidance given on MSDN under “Exception Throwing”:

    Do not use exceptions for normal flow of control, if possible. Except
    for system failures and operations with potential race conditions,
    framework designers should design APIs so that users can write code
    that does not throw exceptions. For example, you can provide a way to
    check preconditions before calling a member so that users can write
    code that does not throw exceptions.

    Here is an example of a bad practice where an exception is handled but can nearly always be avoided:

    public int? GetItem(int index)
    {
        int? value = null;
        try
        {
            value = this.array[index];
        }
        catch (IndexOutOfRangeException)
        {
        }
    
        return value;
    }
    

    This seems contrived but I see code like this quite often from newer programmers. Assuming proper synchronization around reads and writes to array, this exception can be 100% deterministically avoided. Given that, a better way to write that code would be the following:

    public int? GetItem(int index)
    {
        int? value = null;
    
        // Ensure the index is within range in the first place!
        if (index >= 0 && index < this.array.Length)
        {
            value = this.array[index];
        }
    
        return value;
    }
    

    There are other cases where you cannot reasonably avoid exceptions and just need to handle them. This is most commonly encountered when you have to deal with external resources such as files or network connections which you could potentially lose access to or contact with at any time. Example from WCF:

    public void Close()
    {
        // Attempt to avoid exception by doing initial state check
        if (this.channel.State == CommunicationState.Opened)
        {
            try
            {
                // Now we must do a (potentially) remote call;
                // this could always throw.
                this.channel.Close();
            }
            catch (CommunicationException)
            {
            }
            catch (TimeoutException)
            {
            }
        }
    
        // If Close failed, we might need to do final cleanup here.
        if (this.channel.State == CommunicationState.Faulted)
        {
            // local cleanup -- never throws (aside from catastrophic situations)
            this.channel.Abort();
        }
    }
    

    Even in the above example, it’s good to check that the operation you are going to do at least has a chance of succeeding. So there is still an if () check, followed by the appropriate exception handling logic.

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

Sidebar

Related Questions

This question got me thinking about bare strings. When PHP sees a string that's
I got this question from this discussion . A method call like object.m does
This question got me thinking: should we apply the principle that flat is better
Got question. If i make query to server i use something like this: mysql_query(select
I've got question about wpf xaml style definitions. When I try to set style
Just got a question about generics, why doesn't this compile when using a generic
i got a question when create a javascript object, when one function invoking another
I got a question ;) Actually I want to use core-data in different threads.
i got a question about unsigned char array. How can i store an integer
I got this question from my cousin: What will be the best way to

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.