title: Setting an exit code for a custom exception in python
body: <p>I'm using custom exceptions to differ my exceptions from Python's default exceptions.</p>
  
  <p>Is there a way to define a custom exit code when I raise the exception?</p>
  
  <pre><code>class MyException(Exception):
      pass
  
  def do_something_bad():
      raise MyException('This is a custom exception')
  
  if __name__ == '__main__':
      try:
          do_something_bad()
      except:
          print('Oops')  # Do some exception handling
          raise
  </code></pre>
  
  <p>In this code, the main function runs a few functions in a try code.
  After I catch an exception I want to re-raise it to preserve the traceback stack.</p>
  
  <p>The problem is that 'raise' always exits 1.
  I want to exit the script with a custom exit code (for my custom exception), and exit 1 in any other case.</p>
  
  <p>I've looked at this solution but it's not what I'm looking for:
  <a href="https://stackoverflow.com/questions/6720119/setting-exit-code-in-python-when-an-exception-is-raised">Setting exit code in Python when an exception is raised</a></p>
  
  <p>This solution forces me to check in every script I use whether the exception is a default or a custom one.</p>
  
  <p>I want my custom exception to be able to tell the raise function what exit code to use.</p>