Table of Contents Previous Next
Logo
Client-Side Slice-to-Python Mapping : 22.13 Exception Handling
Copyright © 2003-2010 ZeroC, Inc.

22.13 Exception Handling

Any operation invocation may throw a run-time exception (see Section 22.10) and, if the operation has an exception specification, may also throw user excep­tions (see Section 22.9). Suppose we have the following simple interface:
exception Tantrum {
    string reason;
};

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

try:
    child.askToCleanUp()
except Tantrum, t:
    print "The child says:", t.reason
Typically, you will catch only a few exceptions of specific interest around an oper­ation invocation; other exceptions, such as unexpected run-time errors, will usually be handled by exception handlers higher in the hierarchy. For example:
import traceback, Ice

def run():
    child = ...        # Get child proxy...
    try:
        child.askToCleanUp()
    except Tantrum, t:
        print "The child says:", t.reason
        child.scold()  # Recover from error...
    child.praise()     # Give positive feedback...

try:
    # ...
    run()
    # ...
except Ice.Exception:
    traceback.print_exc()
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 3.)

Table of Contents Previous Next
Logo