Throwing null instead of an Exception in Java

11th Mar 2008


As it's usual these days, I was spending the evening doing some extremely interesting programming in Java. It occurred to me all of a sudden, if we could compile a Java program if it threw nulls instead of exceptions!

Consider the following lines of code.

    try
    {
        throw null;
    }
    catch(NullPointerException e)
    {
        System.err.println("Caught in NullPointerException block");
    }
    catch(Exception e)
    {
        System.err.println("Caught in Exception block");
    }
    catch(Throwable t)
    {
        System.err.println("Caught in Throwable block");
    }
    

Listing 1: Java code showing null being thrown.

At a first look, what do you think the result would be? Will the program compile, let alone produce an output? If it did compile somehow, to which catch block would the control be transferred? To confuse you even further, let me tell you that all of the following expressions evaluate to false!

    System.out.println(null instanceof NullPointerException);
    System.out.println(null instanceof Exception);
    System.out.println(null instanceof Throwable);
    

Listing 2: Finding the type of null.

Well, here comes the big surprise! The result of execution of Listing 1 is actually, "Caught in NullPointerException block". Thus, the listing not only compiled fine, it also produced a decent output. The control got handed over to the NullPointerException catch block, somehow! It is as if the null got miraculously transformed into a NullPointerException instance. The behaviour is identical to what we would have obtained if instead of throw null;, we had throw new NullPointerException(); inside the try block.



Explanation

This is not at all an anomaly in Java given that the behaviour is neatly documented in the Java Language Specification. Here I'm quoting a few lines from §14.18 of the JLS, 3rd Edition.


ThrowStatement:
    throw Expression;

  ...

A throw statement first evaluates the Expression. If the evaluation of the Expression completes abruptly for some reason, then the throw completes abruptly for that reason. If evaluation of the Expression completes normally, producing a non-null value V, then the throw statement completes abruptly, the reason being a throw with value V. If evaluation of the Expression completes normally, producing a null value, then an instance V' of class NullPointerException is created and thrown instead of null. The throw statement then completes abruptly, the reason being a throw with value V'.

As explained above, in our case, the expression throw null gets converted to an instance of NullPointerException before being caught by the first catch block. Quite straightforward now, isn't it?

Although such a scenario would be very rare in real life programming, it's great to see the extent of forethought the creators of Java had. Java continues to inspire me, as always!



What others say

Dinesh 14th March 2008
Hey Adarsh,

The statement "Finding the type of null" made me think a little deep...

considering the statement "throw <expression>"

A valid <expression> for the above statement should be of type Throwable or its subclasses

Hence, if I say "null" is of Throwable type(or precisely itz subclass NullPointerException), then one of the following statments shoud have returned true

System.out.println(null instanceof NullPointerException);
System.out.println(null instanceof Exception);
System.out.println(null instanceof Throwable);

Why is it "false" for all the statements above??

Actually, alls the SOP statements above have invalid(i mean "meaningless :-)") expressions(at runtime), though they are compile time friendly ones : just because instance references can be null for some internal jvm memory management errors, and also for the sake of not breaking the rule of "instanceof" operator.

Good thoughts buddy!!!

Cheers Dinesh

Hey buddy,

My earlier (and first) comments were lucky enough to get placed here :( due to some errors, they vanished.

A good write up and an excellent piece of info.. keep it up.

one suggestion is that instead of giving the link to the index of JLS, you could give the appropriate section in it.

Best Wishes..

Cheers,
Raghavan alias Saravanan M.

Raghavan 18th March 2008
@Dinesh,

//Hence, if I say "null" is of Throwable type(or precisely itz subclass NullPointerException), then one of the following statments shoud have returned true

System.out.println(null instanceof NullPointerException);
System.out.println(null instanceof Exception);
System.out.println(null instanceof Throwable);

Why is it "false" for all the statements above??//

That was a good insight. But here you go..

The literal "null" is getting converted of type NullPointerException at runtime when it is being thrown (used in conjunction with the 'throws' statement) - as adarsh demonstrated and with the JLS's justifications.

It is NOT the same at compile time. since a literal null is not equal to anything else. Simple. that's the reason you get false for all sort of comparisons.

This line proves the same

System.out.println(null instanceof Object);

Hope it is convincing.

Cheers,
Raghavan alias Saravanan M.

Carlo V. Pasaol II 16th April 2008
Good info...

Since we are somewhat towards the topic of exceptions....

Why do I get this problem where an exception is thrown and caught but the exception itself is null?

Adarsh R 16th April 2008
Well, Carlo, to be realistic, this kind of a problem never arises practically. Most of the times, exceptions are thrown by creating the exception object inline with the throw keyword. That is, a syntax similar to 'throw new Exception()' is used.

However, if in some application, this pattern is relaxed and an external method/factory is used to generate the Exception instances, we might have this problem. Consider the following:

    public Exception getException()
    {
        Exception e = null;
        
        // Do some application logic and generate the exception.
        
        return e;
    }
    
    public void anotherMethod()
    {
        // This is like a factory method that generates different 
        // instances of Exception, conditionally
        
        throw getException();
    }
			

In a case like this, we might end up with nulls in the throw expression provided the implementation of this exception factory is buggy.



Let me know what you think

Thank you. Please note that comments are moderated and will take some time to show up here.
Name (required)
Email (required but never published)
Website
How much is two + three? (kill spam)
Your Comments (2000 characters max) Characters left: 2000