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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:04:04+00:00 2026-05-15T03:04:04+00:00

In the following example we have two different exceptions we want to communicate. //constructor

  • 0

In the following example we have two different exceptions we want to communicate.

//constructor
public Main(string arg){
   if(arg==null)
      throw new ArgumentNullException("arg");

  Thing foo=GetFoo(arg);

  if(foo==null)
     throw new NullReferenceException("foo is null");    
}

Is this the proper approach for both exception types?

  • 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-15T03:04:04+00:00Added an answer on May 15, 2026 at 3:04 am

    The first exception is definitely correct. It’s the second one which is tricky.

    There are two possibilities here:

    • GetFoo() isn’t meant to return null, ever. In that case we’ve basically proven a bug in GetFoo(). I’m not sure of the best exception here, leaving ContractException (from Code Contracts) aside. Basically you want something like ContractException – an exception which means “The world has gone crazy: this isn’t just an externally unexpected result, there’s a bug here.”
    • GetFoo() can legitimately return null, due to arg‘s value. In this case I would suggest that ArgumentException (but not ArgumentNullException) may be appropriate. On the other hand, it’s odd to throw ArgumentException after using the argument.

    InvalidOperationException isn’t quite appropriate here, but I might be tempted to use it as the closest thing to a contract failure…

    EDIT: You should also consider creating your own exception, as per Aaronaught’s answer.

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

Sidebar

Related Questions

In the following example I have two years worth of data denoted by data_2007
Consider the following example. I have an interface MyInterface, and then two abstract classes
For example suppose I have the following two functions function a(param1) { console.log(param1); }
Say for example, I have the following two tables: TableA { _id } TableB
For example, let's say I have the following two tables: Table1: Id ---- 1
Following is the example I have reproduced: charName :: Char -> String charName 'a'
Let's have a following example: public class X { } public class Y {
I have two entities like the following: @Entity public class Trip { @OneToMany(mappedBy =
I have two arrays of different length. I want to put them into hash,
I'm trying to figure out how XSLT process namespace prefixes and have following example:

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.