---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 19 Edition"
java_version: 19
chapter: "Chapter 14. Blocks, Statements, and Patterns"
section: "14.18"
heading: "14.18. The throw Statement"
parent: "Chapter 14. Blocks, Statements, and Patterns"
source: https://docs.oracle.com/javase/specs/jls/se19/html/jls-14.html#jls-14.18
id: "14.18"
url: https://jvm.obfuscated.software/jls/se19/14.18.html
raw: https://jvm.obfuscated.software/jls/se19/14.18.md
---

# 14.18. The `throw` Statement

A `throw` statement causes an exception ([§11 (*Exceptions*)](11.md)) to be thrown. The result is an immediate transfer of control ([§11.3](11.3.md)) that may exit multiple statements and multiple constructor, instance initializer, static initializer and field initializer evaluations, and method invocations until a `try` statement ([§14.20](14.20.md)) is found that catches the thrown value. If no such `try` statement is found, then execution of the thread ([§17 (*Threads and Locks*)](17.md)) that executed the `throw` is terminated ([§11.3](11.3.md)) after invocation of the `uncaughtException` method for the thread group to which the thread belongs.

```
ThrowStatement:
  throw Expression ;
```

The *Expression* in a `throw` statement must either denote a variable or value of a reference type which is assignable ([§5.2](5.2.md)) to the type `Throwable`, or denote the null reference, or a compile-time error occurs.

The reference type of the *Expression* will always be a class type (since no interface types are assignable to `Throwable`) which is not parameterized (since a subclass of `Throwable` cannot be generic ([§8.1.2](8.1.2.md))).

At least one of the following three conditions must be true, or a compile-time error occurs:

- The type of the *Expression* is an unchecked exception class ([§11.1.1](11.1.1.md)) or the null type ([§4.1](4.1.md)).
- The `throw` statement is contained in the `try` block of a `try` statement ([§14.20](14.20.md)) and it is not the case that the `try` statement can throw an exception of the type of the *Expression*. (In this case we say the thrown value is *caught* by the `try` statement.)
- The `throw` statement is contained in a method or constructor declaration and the type of the *Expression* is assignable ([§5.2](5.2.md)) to at least one type listed in the `throws` clause ([§8.4.6](8.4.6.md), [§8.8.5](8.8.5.md)) of the declaration.

The exception types that a `throw` statement can throw are specified in [§11.2.2](11.2.2.md).

A `throw` statement first evaluates the *Expression*. Then:

- If 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'`.

It can be seen, then, that a `throw` statement always completes abruptly.

If there are any enclosing `try` statements ([§14.20](14.20.md)) whose `try` blocks contain the `throw` statement, then any `finally` clauses of those `try` statements are executed as control is transferred outward, until the thrown value is caught. Note that abrupt completion of a `finally` clause can disrupt the transfer of control initiated by a `throw` statement.

If a `throw` statement is contained in a method declaration or a lambda expression, but its value is not caught by some `try` statement that contains it, then the invocation of the method completes abruptly because of the `throw`.

If a `throw` statement is contained in a constructor declaration, but its value is not caught by some `try` statement that contains it, then the class instance creation expression that invoked the constructor will complete abruptly because of the `throw` ([§15.9.4](15.9.4.md)).

If a `throw` statement is contained in a static initializer ([§8.7](8.7.md)), then a compile-time check ([§11.2.3](11.2.3.md)) ensures that either its value is always an unchecked exception or its value is always caught by some `try` statement that contains it. If at run time, despite this check, the value is not caught by some `try` statement that contains the `throw` statement, then the value is rethrown if it is an instance of class `Error` or one of its subclasses; otherwise, it is wrapped in an `ExceptionInInitializerError` object, which is then thrown ([§12.4.2](12.4.2.md)).

If a `throw` statement is contained in an instance initializer ([§8.6](8.6.md)), then a compile-time check ([§11.2.3](11.2.3.md)) ensures that either its value is always an unchecked exception or its value is always caught by some `try` statement that contains it, or the type of the thrown exception (or one of its superclasses) occurs in the `throws` clause of every constructor of the class.

By convention, user-declared throwable types should usually be declared to be subclasses of class `Exception`, which is a subclass of class `Throwable` ([§11.1.1](11.1.1.md)).
