---
title: "Exceptions"
url: https://docs.zeroc.com/ice/3.8/slice/exceptions
language: java
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Exceptions

Programming language: Java. Other languages: [C++](https://docs.zeroc.com/ice/3.8/slice/exceptions/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/slice/exceptions/csharp.md), [JavaScript](https://docs.zeroc.com/ice/3.8/slice/exceptions/js.md), [MATLAB](https://docs.zeroc.com/ice/3.8/slice/exceptions/matlab.md), [PHP](https://docs.zeroc.com/ice/3.8/slice/exceptions/php.md), [Python](https://docs.zeroc.com/ice/3.8/slice/exceptions/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/slice/exceptions/ruby.md), [Swift](https://docs.zeroc.com/ice/3.8/slice/exceptions/swift.md).

## Exception Syntax and Semantics

A Slice exception defines an error that an operation can report to its caller. Exceptions defined in Slice are called *user exceptions*, to distinguish them from the [exceptions of the Ice runtime](https://docs.zeroc.com/ice/3.8/runtime/local-and-dispatch-exceptions/java.md). For example:

```slice
struct TimeOfDay
{
    short hour;         // 0 - 23
    short minute;       // 0 - 59
    short second;       // 0 - 59
}

exception TimeException {} // Empty exceptions are legal

exception RangeException
{
    TimeOfDay errorTime;
    TimeOfDay minTime;
    TimeOfDay maxTime;
}
```

A user exception is much like a structure in that it contains a number of fields. However, unlike structures, exceptions can have zero fields, that is, be empty. Like classes, user exceptions support inheritance and may include [optional fields](https://docs.zeroc.com/ice/3.8/slice/fields/java.md).

## Exception Specification in Operations

Exceptions allow you to return an arbitrary amount of error information to the client if an error condition arises in the implementation of an operation. Operations use an exception specification to indicate the exceptions that may be returned to the client:

```slice
interface Clock
{
    idempotent TimeOfDay getTime();
    idempotent void setTime(TimeOfDay time)
        throws RangeException, TimeException;
}
```

`setTime` can throw a `RangeException` or a `TimeException`. A `RangeException` reports the `TimeOfDay` value that caused the error and the range of legal values; a `TimeException` reports only that `setTime` failed.

To indicate that an operation does not throw any user exception, simply omit the exception specification. (There is no empty exception specification in Slice.)

### Restrictions for User Exceptions

An exception is not a data type: you cannot use an exception as the type of a parameter, a return value, a field, a sequence element, or a dictionary key or value. Conversely, an exception specification lists only exceptions.

## Exception Inheritance

Slice exceptions support single inheritance. A derived exception inherits the fields of its base exception and can add its own, but cannot redefine an inherited field:

```slice
exception BaseException
{
    string reason;
}

exception DerivedException extends BaseException
{
    int code;
}

interface Example
{
    // May throw BaseException or DerivedException
    void op() throws BaseException;
}
```

An operation can throw any exception derived from the exceptions in its exception specification: `op` can throw a `BaseException` or a `DerivedException`. The language mappings preserve the exception hierarchy, so you can catch a base exception generically or handle specific derived exceptions.

As a system evolves, new derived exceptions are often added to an existing hierarchy. Suppose that you add `FatalApplicationException` to the original definitions and redeploy the server:

```slice
exception AppException
{
    // ...
}

exception FatalApplicationException extends AppException
{
    // ...
}

interface Application
{
    void doSomething() throws AppException;
}
```

A client built with the original definition does not know `FatalApplicationException`: Ice slices the exception to `AppException`, the most-derived type this client knows.

See [Slicing Values and Exceptions](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slicing-values-and-exceptions/java.md) for the rules Ice follows when it slices an exception.

## Language Mapping

A Slice exception is mapped to a Java class with the same name. This mapping is similar to the mapping of [classes](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/java.md).

Consider the following Slice exceptions:

```slice
module M
{
    exception GenericException
    {
        string reason;
    }

    exception BadTimeValException extends GenericException {}
}
```

The Slice compiler generates the following code for these exceptions:

```java
public class GenericException extends com.zeroc.Ice.UserException {
    public String reason;

    public GenericException() {
        this.reason = "";
    }

    public GenericException(String reason) {
        this.reason = reason;
    }

    // ...
}

public class BadTimeValException extends GenericException {
    public BadTimeValException() {
    }

    public BadTimeValException(String reason) {
        super(reason);
    }

    // ...
}
```

There are a number of things to note about this generated code:

1. The generated class `GenericException` inherits from `UserException`. `UserException` is the ultimate ancestor of all mapped exceptions. It’s a checked exception that derives from `java.lang.Exception`.
2. The generated class contains a public field for each Slice field.
3. The generated class for `BadTimeValException` derives from the generated class `GenericException`.
4. The generated class provides a constructor with a parameter for each field and a parameterless constructor; they are identical to the generated constructors for classes. See [Java Mapping for Classes](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/java.md).

### Exception Specification

When an Slice operation has an exception specification, the corresponding client-side and server-side methods in Java have an exception specification. This is true for all mapped methods, except the proxy `Async` methods.

For example:

```slice
interface Greeter
{
    ["amd"]
    string greet(string name) throws BadNameException, GoneFishingException;
}
```

maps to:

```java
// Client-side
public interface GreeterPrx extends com.zeroc.Ice.ObjectPrx {
    default String greet(String name)
        throws BadNameException, GoneFishingException {
        ...
    }

    default String greet(String name, java.util.Map<String, String> context)
        throws BadNameException, GoneFishingException {
        ...
    }

    // No exception specification
    default CompletableFuture<String> greetAsync(String name) {
        ...
    }

    default CompletableFuture<String> greetAsync(String name, java.util.Map<String, String> context) {
        ...
    }
}

// Server-side
public interface Greeter extends com.zeroc.Ice.Object {
    CompletionStage<String> greetAsync(String name, com.zeroc.Ice.Current current)
        throws BadNameException, GoneFishingException;
}
```

> **Note**
>
> If you rename the exception or an enclosing module with `java:identifier`, or place the enclosing top-level module in another Java package with `java:package`, install a [Slice loader](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/java.md) in communicators that receive this exception. For `java:package` alone, setting the matching [Ice.Package.*module*](https://docs.zeroc.com/ice/3.8/property-reference/ice-properties/java.md#ice.package.module) property or [Ice.Default.Package](https://docs.zeroc.com/ice/3.8/property-reference/ice-default-properties/java.md#ice.default.package) also works.

## See Also

- [Local and Dispatch Exceptions](https://docs.zeroc.com/ice/3.8/runtime/local-and-dispatch-exceptions/java.md)
