---
title: "Slicing Values and Exceptions"
url: https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slicing-values-and-exceptions
docs_index: https://docs.zeroc.com/llms.txt
---

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

# Slicing Values and Exceptions

Parts of this page are for some programming languages only, and name them. For one language's text alone, see [C++](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slicing-values-and-exceptions/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slicing-values-and-exceptions/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slicing-values-and-exceptions/java.md), [JavaScript](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slicing-values-and-exceptions/js.md), [MATLAB](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slicing-values-and-exceptions/matlab.md), [PHP](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slicing-values-and-exceptions/php.md), [Python](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slicing-values-and-exceptions/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slicing-values-and-exceptions/ruby.md), and [Swift](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slicing-values-and-exceptions/swift.md).

This page describes the concept of *slicing*, which is how the Ice runtime reacts when it receives an instance of an unknown [class](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes.md) or [exception](https://docs.zeroc.com/ice/3.8/slice/exceptions.md).

## Composition Using Slices

Classes and exceptions are composed of slices, where each slice corresponds to a level in the type hierarchy and contains the data members defined at that level. Consider this example showing a class hierarchy:

```slice
class A
{
    int i;
}

class B extends A
{
    float f;
}

class C extends B
{
    string s;
}
```

An instance of class `C` contains the following slices, listed in order from most-derived to least-derived:

| **Slice** | **Contents** |
| --- | --- |
| `C` | `string s` |
| `B` | `float f` |
| `A` | `int i` |

A receiver *knows* a type when its [Slice loader](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders.md) creates an instance of this type from its type ID. A receiver that knows the most-derived type of an instance, here `C`, unmarshals all its slices. Otherwise, what the receiver does depends on the format the sender used to marshal the instance:

- **Compact format** The receiver must know the most-derived type of the instance. If it does not, Ice throws a `MarshalException`.
- **Sliced format** The receiver skips the slices of the types it does not know, also known as *slicing* the instance, and creates an instance of the most-derived type it knows. This instance must still be an instance of the declared type, such as the return type of an operation; otherwise, Ice throws a `MarshalException`. If the receiver knows none of the types of a class instance, Ice keeps it as an [unknown sliced value](#unknown-sliced-values), which only the declared type `Value` accepts.

These rules also apply to an exception, where the declared type is the operation's [exception specification](https://docs.zeroc.com/ice/3.8/slice/exceptions.md#exception-specification-in-operations): after slicing, the exception must be one of the declared exceptions or derive from one; otherwise, Ice throws an `UnknownUserException`. If the receiver knows none of the exception's types, Ice throws a `MarshalException`.

## Slice Formats

Ice provides two on-the-wire formats for class and exception slices: the compact format and the sliced format. For class instances, Ice uses the compact format by default, which is more space-efficient on the wire but offers less flexibility on the receiving end. Ice always marshals exceptions in the sliced format: the `format` metadata and the `Ice.Default.SlicedFormat` property described below apply to class instances only.

An application that needs the slicing behavior we discussed in the previous section for class instances must explicitly enable the sliced format as follows:

- Set the [Ice.Default.SlicedFormat](https://docs.zeroc.com/ice/3.8/property-reference/ice-default-properties.md#ice.default.slicedformat) property to `1` to use the sliced format by default.
- Annotate your Slice definitions with the `format:sliced` [metadata](https://docs.zeroc.com/ice/3.8/slice/slice-metadata-directives.md) to selectively enable the sliced format for certain operations or interfaces.

For example, suppose an application can safely use the compact format most of the time, but still needs slicing in a few situations. In this case the application can use metadata to enable the sliced format where necessary:

```slice
interface Ledger
{
    Account getAccount(string id); // Uses compact format

    ["format:sliced"]
    Account importAccount(string source); // Uses sliced format
}
```

The caller of `getAccount` is expected to know every type this operation can return, while the caller of `importAccount` may not. With the sliced format, Ice slices an instance of a derived class this caller does not know to the most-derived type it knows, which can be `Account` itself.

The `format` metadata on an operation overrides the one on its interface. For example, all operations of this `Ledger` use the sliced format, except `getAccount`:

```slice
["format:sliced"]
interface Ledger
{
    ["format:compact"]
    Account getAccount(string id); // Uses compact format

    Account importAccount(string source); // Uses sliced format
}
```

The format affects the marshaling of input parameters, output parameters, and return value of an operation.

Consider this example:

```slice
exception IncompatibleAccount { ... }

interface Ledger
{
    ["format:compact"]
    Account migrateAccount(Account oldAccount) throws IncompatibleAccount;
}
```

The metadata forces the client to use the compact format for the input parameter `oldAccount`, and forces the server to use the compact format for the return value.

If you decide to use the `Ice.Default.SlicedFormat` property, be aware that this property only affects the sender of a value. For example, if you enable this property in the client but not the server, then all values sent by the client use the sliced format by default, but all values returned by the server use the compact format by default.

By offering two alternative formats, Ice gives you a great deal of flexibility in designing your applications. The compact format is ideal for applications that place a greater emphasis on efficiency, while the sliced format is helpful when clients and servers evolve independently.

## Preserving Slices

When Ice slices a class instance, it preserves the slices of unknown types; when it slices an exception, it does not. Here is a simple example:

```slice
class Base
{
    int b;
}

class Intermediate extends Base
{
    int i;
}

class Derived extends Intermediate
{
    int d;
}

interface Relay
{
    ["format:sliced"]
    Base transform(Base b);
}
```

The server implementing the `Relay` interface must know the type `Base` (because it is statically referenced in the interface definition), but may not know `Intermediate` or `Derived`. Suppose the implementation of `transform` involves forwarding the instance to another back-end server for processing and returning the transformed instance to the caller. In effect, the `Relay` server is an intermediary. If the `Relay` server does not know the types `Intermediate` and `Derived`, it slices an instance of `Derived` to `Base` and preserves the slices of `Intermediate` and `Derived` in marshaled form. When the `Relay` server marshals this instance in the sliced format, Ice writes the preserved slices back, so the back-end server receives all the slices of the `Derived` instance.

When the `Relay` server marshals this instance in the compact format, Ice omits the preserved slices and writes only the `Base` slice.

If a class instance is sliced upon receipt, calling `ice_getSlicedData` on this instance will return a SlicedData ([C++](https://code.zeroc.com/ice/3.8/api/cpp/classIce_1_1SlicedData.html), [C#](https://code.zeroc.com/ice/3.8/api/csharp/api/Ice.SlicedData.html), [Java](https://code.zeroc.com/ice/3.8/api/java/com.zeroc.ice/com/zeroc/Ice/SlicedData.html), [JavaScript](https://code.zeroc.com/ice/3.8/api/javascript/Ice/SlicedData.html), [PHP](https://code.zeroc.com/ice/3.8/api/php/classes/Ice-SlicedData.html), [Python](https://code.zeroc.com/ice/3.8/api/python/Ice.SlicedData.html), [Ruby](https://code.zeroc.com/ice/3.8/api/ruby/Ice/SlicedData.html), and [Swift](https://code.zeroc.com/ice/3.8/api/swift/documentation/ice/sliceddata)) object that holds the preserved slices.

### Unknown Sliced Values

Suppose we change the `transform` operation of the `Relay` example to use `Value`:

```slice
interface Relay
{
    ["format:sliced"]
    Value transform(Value b);
}
```

The intermediary server then does not need to know *any* of the class types it relays, because the types in the signature of `transform` impose no requirement. As long as `transform` uses the sliced format, this intermediary can relay values of any type.

If the Ice runtime in the intermediary does not know any of the types in an object's inheritance hierarchy, and the formal type is `Value`, Ice uses an instance of UnknownSlicedValue ([C++](https://code.zeroc.com/ice/3.8/api/cpp/classIce_1_1UnknownSlicedValue.html), [C#](https://code.zeroc.com/ice/3.8/api/csharp/api/Ice.UnknownSlicedValue.html), [Java](https://code.zeroc.com/ice/3.8/api/java/com.zeroc.ice/com/zeroc/Ice/UnknownSlicedValue.html), [JavaScript](https://code.zeroc.com/ice/3.8/api/javascript/Ice/UnknownSlicedValue.html), [PHP](https://code.zeroc.com/ice/3.8/api/php/classes/Ice-UnknownSlicedValue.html), [Python](https://code.zeroc.com/ice/3.8/api/python/Ice.UnknownSlicedValue.html), [Ruby](https://code.zeroc.com/ice/3.8/api/ruby/Ice/UnknownSlicedValue.html), and [Swift](https://code.zeroc.com/ice/3.8/api/swift/documentation/ice/unknownslicedvalue)) to represent the instance.

The implementation of `transform` receives an instance of `UnknownSlicedValue` and can use that object as its return value. If necessary, the implementation can determine the most-derived type of the instance by calling `ice_id`.
