What happens when you write `throw null;` in Java, and why?
answer
- throw null compiles (null fits Throwable type)
- at runtime -> NullPointerException, always
- JLS: throwing null behaves as throwing an NPE
- misleading: stack trace points at throw, not real bug
- guard with Objects.requireNonNull or inline new
basics
~10 sthrow null compiles, but at runtime Java cannot raise a null exception, so it throws a NullPointerException instead. The same thing happens if a variable you throw happens to be null.
solid answer
~40 s`throw` requires a non-null Throwable. Writing `throw null;` (or throwing a reference that is null at runtime) compiles fine because `null` is assignable to the Throwable type, but the JVM cannot raise a non-existent object. The Java Language Specification says throwing a null reference behaves as if you had thrown a `NullPointerException`. So instead of the exception you intended, the caller sees an NPE — often with a confusing stack trace pointing at the throw site rather than the real bug. This commonly bites people when a method returns or builds an exception object that turns out to be null (e.g., a factory that returned null), and that null is then thrown. The fix is to ensure the thrown reference is always a real, constructed Throwable.
go deeper
Knows that throwing null gives a NullPointerException rather than the intended exception.
Explains why it compiles (null fits any reference type) and that the JLS mandates the NPE substitution.
Connects the trap to misleading stack traces and upstream null-returning factories, and prescribes defensive construction/validation.
Treats null-Throwable as an API-contract smell and drives conventions/static analysis to prevent null exceptions across the codebase.
## Setup: what throw expects The `throw` statement takes an expression that must be of type `Throwable` (or a subtype). Java's type system treats the literal `null` as compatible with *any* reference type, including `Throwable`. So both of these **compile without complaint**: ```java throw null; // literal null Throwable t = maybeNull(); // could return null throw t; // t might be null at runtime ``` ## The runtime problem At runtime, `throw` needs an actual object to propagate — an exception carries a type, a message, and a stack trace. A `null` reference points to **no object at all**, so there is nothing to raise. The Java Language Specification resolves this by mandating: *if the evaluated expression is `null`, a `NullPointerException` is thrown instead.* In effect: ```java throw null; // behaves like: throw new NullPointerException(); ``` So the program **does** fail with an exception — just not the one the author might have expected. You always get a `NullPointerException`. ## Why this is a trap The danger is **misleading diagnostics**. Imagine: ```java Exception error = lookupError(code); // returns null for an unknown code if (error != null) { // ... } // later, mistakenly: throw error; // if error is null -> NullPointerException, not the domain error ``` The stack trace points at the `throw` line. A developer reading it may think the NPE is a bug *at the throw site*, when the real defect is upstream — a factory or lookup that produced `null`. The intended exception type and its message are entirely lost. ## Defining the terms - **`Throwable`**: the superclass of everything that can be thrown (`Exception`, `Error`, and their subclasses). - **`null`**: a reference value that points to no object. - **`NullPointerException` (NPE)**: the exception Java raises when you try to use a reference as if it pointed to an object, but it is `null` (calling a method on it, accessing a field, indexing it — or, here, throwing it). ## How to avoid it - Never throw a reference you haven't proven non-null. Prefer `throw new SomeException(...)` with an inline constructor call so it can't be null. - If you must throw a variable, validate it: `Objects.requireNonNull(ex)` first, or guard with an `if`. - Be suspicious of factories/lookups that can return `null` exceptions; have them return a default exception or never return null. ## Takeaway `throw null` is legal to compile but is effectively `throw new NullPointerException()`. The requirement is that the thrown Throwable be non-null; violating it doesn't crash the compiler, it silently converts your intent into an NPE.
- Does throw null cause a compile error or a runtime error?A runtime error. It compiles because null is assignable to Throwable, but at runtime it raises a NullPointerException.
- How can you defensively prevent accidentally throwing null?Construct exceptions inline (throw new X(...)) so the reference can't be null, or validate a thrown variable with Objects.requireNonNull before throwing it.
saying these in an interview costs you the question
- Saying throw null is a compile error (it compiles)
- Thinking throw null throws nothing / is a no-op
- Expecting the originally intended exception type to survive
- Forgetting that a null variable, not just the literal, triggers this