In gRPC Java, io.grpc.StatusRuntimeException is the unchecked exception that carries a gRPC Status (a status code, an optional description and optional metadata trailers). Servers create it to tell a client that a call failed, and clients catch it to find out why. It is the standard way to map application errors to the fixed set of gRPC status codes, such as INVALID_ARGUMENT or NOT_FOUND.
A closely related class, StatusException, carries the same information but is a checked exception. Which one you use depends mostly on whether you want the compiler to force callers to handle it.
Every gRPC call finishes with a status. A successful call ends with OK. A failed call ends with one of the canonical codes and, optionally, a human-readable description. In Java the io.grpc.Status class models this, and StatusRuntimeException wraps a Status so it can be thrown or passed around.
Commonly used codes:
| Code | Use it when |
|---|---|
INVALID_ARGUMENT | The request is malformed or fails validation, regardless of system state |
NOT_FOUND | The requested entity does not exist |
ALREADY_EXISTS | The entity you tried to create already exists |
PERMISSION_DENIED | The caller is authenticated but not allowed |
UNAUTHENTICATED | Credentials are missing or invalid |
FAILED_PRECONDITION | The system is not in a state that allows the operation |
DEADLINE_EXCEEDED | The deadline passed before the call finished |
UNAVAILABLE | The service is temporarily unreachable, so retrying may work |
INTERNAL | An unexpected server bug |
Choosing the right code matters because clients, retry policies and load balancers act on it.
In a server you normally do not throw from the handler. You send the error through the StreamObserver so the call completes properly:
asRuntimeException() builds the StatusRuntimeException. If an unexpected exception escapes the handler, grpc-java already converts it to Status.UNKNOWN, but it is better to map it yourself to INTERNAL and log the cause on the server. Do not put stack traces or sensitive data in the description, because it is sent to the client. withCause() is not transmitted to the client, it only helps local logging.
A blocking stub throws StatusRuntimeException when the call fails. Read the code with getStatus():
With asynchronous stubs, the error arrives in StreamObserver.onError(Throwable t). Use Status.fromThrowable(t) to get the Status safely, since the throwable can be a StatusRuntimeException or something else.
A status code and a string are often not enough. gRPC supports the richer error model from Google's API design guidelines, using google.rpc.Status with typed detail messages such as BadRequest. In Java, io.grpc.protobuf.StatusProto converts between the two:
On the client, StatusProto.fromThrowable(e) returns the google.rpc.Status, and you unpack the details with Any.unpack. Simpler cases can attach key-value pairs through io.grpc.Metadata trailers using Status.asRuntimeException(metadata).
Repeating try/catch in every method is noisy. Spring-based setups often use an exception handler for gRPC, and plain grpc-java users can write a ServerInterceptor that catches exceptions and closes the call with the right status. In Spring gRPC and several community starters you declare a global exception handler class, so check the documentation of the library you use. The goal is the same everywhere: domain exceptions in, gRPC status codes out, in one place.
Both carry a Status. StatusException is checked, StatusRuntimeException is unchecked. Generated blocking stubs throw the runtime version.
No. Only the status code, description, metadata and any detail messages travel over the wire. The cause stays on the server.
Use INVALID_ARGUMENT for malformed requests and FAILED_PRECONDITION when the request is fine but the system state does not allow it.
No. Retry only on codes that mean a transient problem, mainly UNAVAILABLE, ideally with backoff and with idempotent calls.
StatusRuntimeException is how grpc-java expresses a failed call: a canonical status code plus description and optional details. Send it from servers through onError, read getStatus() on clients, choose codes deliberately, keep internal details off the wire and centralise the mapping.