title: How to handle exceptions from any task in an event loop?
body: <p>I would like to stop the program, i.e. the event loop, if any task anywhere raises an unhandled exception. The normal behaviour is that you get a <code>Task exception was never retrieved</code> error message and the individual task stops, but the loop continues to run all other tasks.</p>
  
  <p>For example:</p>
  
  <pre><code>import asyncio
  
  async def foo():
      while True:
          await asyncio.sleep(1)
          print("hi")
  
  async def bug():
      await asyncio.sleep(2)
      raise TypeError
  
  loop = asyncio.get_event_loop()
  loop.create_task(foo())
  loop.create_task(bug())
  loop.run_forever()
  </code></pre>
  
  <hr>
  
  <p>Output:</p>
  
  <pre><code>"hi"
  "hi"
  Task exception was never retrieved
  future: &lt;Task finished coro=&lt;bug() done, defined at &lt;...&gt;:9&gt; exception=TypeError()&gt;
  Traceback (most recent call last):
    File "&lt;ipython-input-4-bd8651340a75&gt;", line 11, in bug
      raise TypeError
  TypeError
  "hi"
  "hi"
  "hi"
  ...
  </code></pre>
  
  <p>My project contains many dozens of coroutines spread over many files which add each other to the loop with <code>loop.create_task(foo())</code> so because they don't <code>await</code> each other, you cannot simply handle one or two entry point coroutines in the main file and have errors bubble up.</p>