strange exceptions.SystemExit in Python 2.7
<p>Here is my code and error message, anyone have any ideas why there are such exception? Thanks.</p>

<p><strong>Source Code</strong>,</p>

<pre><code>import sys
import tensorflow as tf

def main(argv):
  print 'in main'
def f():
    # this method will call def main(argv)
    try:
        tf.app.run()
    except:
        print "tf.app.run error ", sys.exc_info()

if __name__ == "__main__":
    f()
</code></pre>

<p><strong>Error Code</strong>,</p>

<pre><code>in main
tf.app.run error  (&lt;type 'exceptions.SystemExit'&gt;, SystemExit(), &lt;traceback object at 0x10fa33f38&gt;)
</code></pre>

<p>It's coming from the <a href="https://docs.python.org/3/library/sys.html#sys.exit" rel="nofollow"><code>sys.exit()</code></a> call, about which the following is said:</p>

<blockquote>
  <p>Since exit() ultimately âonlyâ raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.</p>
</blockquote>

<p>In your case, <code>sys.exit</code> seems to be called from the function <a href="https://github.com/tensorflow/tensorflow/blob/f6990bffe06ac38f6cc4595eade32f0e0af4113d/tensorflow/python/platform/app.py#L30" rel="nofollow"><code>run()</code></a> unconditionally, so avoid intercepting <code>SystemExit</code>. </p>

<p>If you want to handle all kinds of application-relevant exception, try catching <code>Exception</code> instead of the bare <code>except</code>, since <a href="https://docs.python.org/3/library/exceptions.html#SystemExit" rel="nofollow"><code>SystemExit</code></a>:</p>

<blockquote>
  <p>...inherits from BaseException instead of Exception so that it is not accidentally caught by code that catches Exception. This allows the exception to properly propagate up and cause the interpreter to exit.</p>
</blockquote>

python python-2.7 exception tensorflow