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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:07:30+00:00 2026-05-13T15:07:30+00:00

While writing some particularly complex exception handling code, someone asked, don’t you need to

  • 0

While writing some particularly complex exception handling code, someone asked, don’t you need to make sure that your exception object isn’t null? And I said, of course not, but then decided to try it. Apparently, you can throw null, but it is still turned into an exception somewhere.

Why is this allowed?

throw null;

In this snippet, thankfully ‘ex’ is not null, but could it ever be?

try
{
  throw null;
}
catch (Exception ex)
{
  //can ex ever be null?

  //thankfully, it isn't null, but is
  //ex is System.NullReferenceException
}
  • 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-13T15:07:30+00:00Added an answer on May 13, 2026 at 3:07 pm

    Because the language specification expects an expression of type System.Exception there (therefore, null is a valid in that context) and doesn’t restrict this expression to be non-null. In general, there’s no way it could detect whether the value of that expression is null or not. It would have to solve the halting problem. The runtime will have to deal with the null case anyway. See:

    Exception ex = null;
    if (conditionThatDependsOnSomeInput) 
        ex = new Exception();
    throw ex; 
    

    They could, of course, make the specific case of throwing the null literal invalid but that wouldn’t help much, so why waste specification space and reduce consistency for little benefit?

    Disclaimer (before I get slapped by Eric Lippert): This is my own speculation about the reasoning behind this design decision. Of course, I haven’t been in the design meeting 😉


    The answer to your second question, whether an expression variable caught within a catch clause can ever be null: While the C# specification is silent about whether other languages can cause a null exception to be propagated, it does define the way exceptions are propagated:

    The catch clauses, if any, are examined in order of appearance to locate a suitable handler for the exception. The first catch clause that specifies the exception type or a base type of the exception type is considered a match. A general catch clause is considered a match for any exception type. […]

    For null, the bold statement is false. So, while purely based on what the C# spec says, we can’t say the underlying runtime won’t ever throw null, we can be sure that even if that’s the case, it’ll be only handled by the generic catch {} clause.

    For C# implementations on the CLI, we can refer to the ECMA 335 specification. That document defines all exceptions that the CLI throws internally (none of which are null) and mentions that user defined exception objects are thrown by the throw instruction. The description for that instruction is virtually identical to C# throw statement (except that it doesn’t restrict the type of the object to System.Exception):

    Description:

    The throw instruction throws the exception object (type O) on the stack and empties the stack. For details of the exception mechanism, see Partition I.
    [Note: While the CLI permits any object to be thrown, the CLS describes a specific exception class that shall be used for language interoperability. end note]

    Exceptions:

    System.NullReferenceException is thrown if obj is null.

    Correctness:

    Correct CIL ensures that object is always either null or an object reference (i.e., of type O).

    I believe these are sufficient to conclude caught exceptions are never null.

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

Sidebar

Ask A Question

Stats

  • Questions 306k
  • Answers 306k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You have invoked undefined behaviour, because it modifies i and… May 13, 2026 at 9:12 pm
  • Editorial Team
    Editorial Team added an answer It seems like for your configuration, container-managed transactions are used… May 13, 2026 at 9:12 pm
  • Editorial Team
    Editorial Team added an answer It's called a Favicon, have a read. <link rel="shortcut icon"… May 13, 2026 at 9:12 pm

Related Questions

I'm working in WPF using the MVVM pattern, and generally things seem to be
I've been a PHP developer for many years now, with many tools under my
Some of my recent web projects that I worked on, use a flow engine
There are many things that all programmers should know, but I am particularly interested
edit alright, I guess C is painful in nature--Just, this part, is particularly painful.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.