Table of Contents Previous Next
Logo
Client-Side Slice-to-C# Mapping : 14.12 Exception Handling
Copyright © 2003-2009 ZeroC, Inc.

14.12 Exception Handling

Any operation invocation may throw a run-time exception (see Section 14.9 on page 443) and, if the operation has an exception specification, may also throw user exceptions (see Section 14.9 on page 443). Suppose we have the following simple interface:
exception Tantrum {
    string reason;
};

interface Child {
    void askToCleanUp() throws Tantrum;
};
Slice exceptions are thrown as C# exceptions, so you can simply enclose one or more operation invocations in a trycatch block:
ChildPrx child = ...;   // Get child proxy...

try
{
    child.askToCleanUp();
}
catch (Tantrum t)
{
    System.Console.Write("The child says: ");
    System.Console.WriteLine(t.reason);
}
Typically, you will catch only a few exceptions of specific interest around an operation invocation; other exceptions, such as unexpected run-time errors, will typically be handled by exception handlers higher in the hierarchy. For example:
public class Client
{
    private static void run() {
        ChildPrx child = ...;       // Get child proxy...
        try
        {
            child.askToCleanUp();
        }
        catch (Tantrum t)
        {
            System.Console.Write("The child says: ");
            System.Console.WriteLine(t.reason);
            child.scold();          // Recover from error...
        }
        child.praise();             // Give positive feedback...
    }

    static void Main(string[] args)
    {
        try
        {
            // ...
            run();
            // ...
        }
        catch (Ice.Exception e)
        {
            System.Console.WriteLine(e);
        }
    }
}
This code handles a specific exception of local interest at the point of call and deals with other exceptions generically. (This is also the strategy we used for our first simple application in Chapter 15.)
Note that the ToString method of exceptions prints the name of the exception, any inner exceptions, and the stack trace. Of course, you can be more selective in the way exceptions are displayed. For example, e.GetType().Name returns the (unscoped) name of an exception.

Exceptions and Out-Parameters

The Ice run time makes no guarantees about the state of out-parameters when an operation throws an exception: the parameter may still have its original value or may have been changed by the operation’s implementation in the target object. In other words, for out-parameters, Ice provides the weak exception guarantee [21] but does not provide the strong exception guarantee.1

1
This is done for reasons of efficiency: providing the strong exception guarantee would require more overhead than can be justified.

Table of Contents Previous Next
Logo