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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:39:06+00:00 2026-06-01T07:39:06+00:00

Lets say there is setup like this: public class MyClass { public void DoSomething(string

  • 0

Lets say there is setup like this:

public class MyClass
{
    public void DoSomething(string Data)
    {
      //if (String.IsNullOrWhiteSpace(Data))
        //throw new NullReferenceException();

      //Do something with Data and let it throw??
    }
}


public class EntryPointClass
{
  public void DoIt(string Data)
  {
     var logicClass = new MyClass();

     try
     {
        logicClass.DoSomething(Data);
     }
     catch(Exception ex)
     {

     }
  }
}

In DoSomething I can detect a problem and throw an exception. In testing EntryPointClass I can test the expected outcome or test that something in happened in the catch.

Why is it better to throw an exception rather than just wait for one to happen? Either way we’ve caught it!

  • 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-01T07:39:07+00:00Added an answer on June 1, 2026 at 7:39 am

    You do both:

    public void DoSomething(string Data)
    {
      if (String.IsNullOrWhiteSpace(Data))
        //throw new NullReferenceException();  
        throw new ArgumentException("Data");
    
    
      //Do something with Data and let it throw??
    }
    

    The aim is to throw early and provide specific information.
    The direct reason to throw here is that the contract with DoSomething() is broken. Signal it, don’t wait for `DoSomething() to carry on and break other contracts.

    Fail fast, fail early.

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

Sidebar

Related Questions

Let's say I have a user class setup with validation like this: [MetadataType(typeof(ssUserMetaData))] public
I encountered a class during my work that looks like this: public class MyObject
My setup goes like this. I'm having a timed buying period let's say for
How does CouchDB handles conflicts while doing bi-directional replication? For example: Lets say there
Lets say that you have a business object whose current state implies that there
Lets say I'm doing a rebase B of a branch onto master and there's
Lets say i have a TextBlock thats bound to a DateTime, is there any
Is there anyway to grab the referring URL using javascript, lets say the reffering
Let's say there's a.gz, and b.gz. $ gzip_merge a.gz b.gz -output c.gz I'd like
Let's say I have this Hello.scala. object HelloWorld { def main(args: Array[String]) { println(Hello,

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.