Tornado how to return error exception?
<p>I want to run a method I know this method doesn't work and I want to get the error returned by the method. </p>

<p>This is my code : </p>

<pre><code>def is_connect(s):
    print("ok connection")
    print(s)
    ioloop.stop()


try:


    current_job_ready = 0
    print("ok1")
    beanstalk = beanstalkt.Client(host='host', port=port)

    print("ok1")
    beanstalk.connect(callback=is_connect)


    ioloop = tornado.ioloop.IOLoop.instance()
    ioloop.start()
    print("ok2")
except IOError as e:
    print(e)
</code></pre>

<p>And this is the error I have when I run my program with wring port : </p>

<pre><code>WARNING:tornado.general:Connect error on fd 7: ECONNREFUSED
ERROR:tornado.application:Exception in callback &lt;functools.partial object at 0x7f5a0eac6f18&gt;
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py", line 604, in _run_callback
    ret = callback()
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py", line 619, in &lt;lambda&gt;
    self.add_future(ret, lambda f: f.result())
  File "/usr/local/lib/python2.7/dist-packages/tornado/concurrent.py", line 237, in result
    raise_exc_info(self._exc_info)
  File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 270, in wrapper
    result = func(*args, **kwargs)
TypeError: connect() takes exactly 1 argument (2 given)
</code></pre>

<p>I want to have e when I enter a false port or host. 
How can I do this? 
I tired to add <code>raise IOError("connection error")</code> after <code>beanstalk = beanstalkt.Client(host='host', port=port)</code> But this force the error, and I just want to have error when it exist.</p>

<p>Here's where reading the code helps. In beanstalkt 0.6's <code>connect</code>, it creates an IOStream to connect to the server:</p>

<p><a href="https://github.com/nephics/beanstalkt/blob/v0.6.0/beanstalkt/beanstalkt.py#L108" rel="nofollow">https://github.com/nephics/beanstalkt/blob/v0.6.0/beanstalkt/beanstalkt.py#L108</a></p>

<p>It registers your callback to be executed on success, but if the connection fails it'll just call <code>Client._reconnect</code> once per second forever. I think you should open a feature request in their GitHub project asking for an error-notification system for <code>connect</code>. With the current beanstalkt implementation, you just have to decide how long you're willing to wait for success:</p>

<pre><code>import sys
from datetime import timedelta

from tornado.ioloop import IOLoop

def is_connect(s):
    print("ok connection")
    print(s)
    loop.remove_timeout(timeout)
    # Do something with Beanstalkd....

def connection_failed():
    print(sys.stderr, "Connection failed!")
    # Could call IOLoop.stop() or just quit.
    sys.exit(1)

loop = IOLoop.current()
timeout = loop.add_timeout(timedelta(seconds=1), connection_failed)
beanstalk.connect(callback=is_connect)
loop.start()
</code></pre>

python exception tornado beanstalk