---
title: "Exceptions"
url: https://docs.zeroc.com/ice/3.8/slice/exceptions
language: php
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: PHP. 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), [Java](https://docs.zeroc.com/ice/3.8/slice/exceptions/java.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), [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/php.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/php.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/php.md) for the rules Ice follows when it slices an exception.

## Language Mapping

A Slice exception is mapped to a PHP 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/php.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:

```php
class GenericException extends \Ice\UserException
{
    public $reason;
    // ...
}

class BadTimeValException extends \M\GenericException
{
    // ..
}
```

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

1. The generated class `GenericException` inherits from `\Ice\UserException`. `\Ice\UserException` is the ultimate ancestor of all mapped exceptions. It derives indirectly from `\Exception`.
2. The generated class contains a public property for each Slice field.
3. The generated class for `BadTimeValException` derives from the generated class `GenericException`.

## See Also

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