Table of Contents Previous Next
Logo
Server-Side Slice-to-C# Mapping : 16.6 Raising Exceptions
Copyright © 2003-2010 ZeroC, Inc.

16.6 Raising Exceptions

To throw an exception from an operation implementation, you simply instantiate the exception, initialize it, and throw it. For example:
// ...
public override void write(string[] text, Ice.Current current)
{
    try
    {
        // Try to write file contents here...
    }
    catch(System.Exception ex)
    {
        GenericError e = new GenericError("cannot write file", ex);
        e.reason = "Exception during write operation";
        throw e;
    }
}
Note that, for this example, we have supplied the optional second parameter to the GenericError constructor (see Section 14.9). This parameter sets the InnerException member of System.Exception and preserves the orig­inal cause of the error for later diagnosis.
// ...

public void
write(String[] text, Ice.Current current)
    throws GenericError

{
    // Try to write file contents here...
    // Assume we are out of space...
    if (error) {
        GenericError e = new GenericError();
        e.reason = "file too large";
        throw e;
    }
}
If you throw an arbitrary C# run-time exception (such as an InvalidCastEx­ception), the Ice run time catches the exception and then returns an UnknownException to the client. Similarly, if you throw an “impossible” user exception (a user exception that is not listed in the exception specification of the operation), the client receives an UnknownUserException.
If you throw an Ice run-time exception, such MemoryLimitException, the client receives an UnknownLocalException.1 For that reason, you should never throw system exceptions from operation implementations. If you do, all the client will see is an UnknownLocalException, which does not tell the client anything useful.

1
There are three run-time exceptions that are not changed to UnknownLocalException when returned to the client: ObjectNotExistException, OperationNotExistException, and FacetNotExistException. We discuss these exceptions in more detail in Chapter 33.


Table of Contents Previous Next
Logo