title: How do I break out of a try loop in python?
body: <p>Please refer to the following code:</p>
  
  <pre><code>import sys
  def x():
      try:
          y()
      except:
          print("exception caught")
  def y():
      sys.exit()
  x()
  </code></pre>
  
  <p>In this instance, the try loop in function <code>x()</code> will be carried over to the function <code>y()</code>, causing the except loop to run due to <code>sys.exit()</code> raising an error. I know, we can change it to raise SystemExit to exit it, but is there a way to break out of the try loop or is there a better way of writing this code?</p>
  
  <p>Thank you for reading and thanks in advance.</p>