Mercurial > pymonkey
comparison docs/src/pymonkey.txt @ 168:68d9e1ed19f8
Added docs for set_throw_hook().
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Sun, 30 Aug 2009 20:31:58 -0700 |
parents | b745c9d8eef9 |
children | 2b98d4643c44 |
comparison
equal
deleted
inserted
replaced
167:b745c9d8eef9 | 168:68d9e1ed19f8 |
---|---|
330 | 330 |
331 If you need to disambiguate between whether ``None`` is the | 331 If you need to disambiguate between whether ``None`` is the |
332 pending exception or there is no pending exception, use | 332 pending exception or there is no pending exception, use |
333 :meth:`is_exception_pending()`. | 333 :meth:`is_exception_pending()`. |
334 | 334 |
335 .. method:: set_throw_hook(func) | |
336 | |
337 Sets the throw hook for the context to the given Python callable. | |
338 The hook is triggered whenever an exception is thrown in the | |
339 context. | |
340 | |
341 `func` takes one argument: the context that triggered it. | |
342 | |
343 For example, here's a throw hook that prints information about | |
344 an exception as it's being propagated through the stack: | |
345 | |
346 >>> def throwhook(cx): | |
347 ... stack = cx.get_stack() | |
348 ... if stack['function']: | |
349 ... where = stack['function'].name | |
350 ... else: | |
351 ... where = stack['script'].filename | |
352 ... exc = cx.get_pending_exception() | |
353 ... print "%s being thrown in %s" % (exc, where) | |
354 | |
355 Here's the hook in action: | |
356 | |
357 >>> cx = pymonkey.Runtime().new_context() | |
358 >>> cx.set_throw_hook(throwhook) | |
359 >>> obj = cx.new_object() | |
360 >>> code = "(function foo() { throw 'oops' })()" | |
361 >>> try: | |
362 ... cx.evaluate_script(obj, code, '<string>', 1) | |
363 ... except pymonkey.error, e: | |
364 ... print "caught %s" % e.args[0] | |
365 oops being thrown in foo | |
366 oops being thrown in <string> | |
367 caught oops | |
368 | |
335 .. method:: set_operation_callback(func) | 369 .. method:: set_operation_callback(func) |
336 | 370 |
337 Sets the operation callback for the context to the given Python | 371 Sets the operation callback for the context to the given Python |
338 callable. The callback can be triggered via | 372 callable. The callback can be triggered via |
339 :meth:`trigger_operation_callback()`. | 373 :meth:`trigger_operation_callback()`. |