title: Flutter catching all unhandled exceptions
body: <p>I'm trying to catch all unhandled exceptions in a Flutter app so I can sent it to a crash reporter. There are <a href="https://flutter.dev/docs/testing/errors" rel="noreferrer">instructions on how to do this in the Flutter docs</a>. I followed those, and added two bits of code to my app to catch exceptions:</p>
  <p>Catch Dart errors by wrapping <code>runApp</code> in <code>runZoned</code>:</p>
  <pre><code>runZoned&lt;Future&lt;void&gt;&gt;(
    () async {
      runApp(MyApp());
    },
    onError: (dynamic error, StackTrace stackTrace) {
      print(&quot;=================== CAUGHT DART ERROR&quot;);
      // Send report
    },
  );
  </code></pre>
  <p>Catch flutter errors by setting <code>FlutterError.onError</code>:</p>
  <pre><code>FlutterError.onError = (FlutterErrorDetails details) {
    print(&quot;=================== CAUGHT FLUTTER ERROR&quot;);
    // Send report
  };
  </code></pre>
  <p>However, when I test this at runtime by throwing an exception from a button:</p>
  <pre><code>throw Exception(&quot;Just testing&quot;);
  </code></pre>
  <p>The exception appears in the console:</p>
  <blockquote>
  <p>════════ Exception Caught By gesture
  ═══════════════════════════════════════════════════════════════
  The following _Exception was thrown while handling a gesture: Exception:
  Just testing When the exception was
  thrown, this was the stack:</p>
  <p>... etc</p>
  </blockquote>
  <p>But I see no sign of my print statements (CAUGHT DART ERROR or CAUGHT FLUTTER ERROR), and setting breakpoints on those lines never seems to hit, so I think my  exception handling code isn't catching it. Am I missing something?</p>
  <p>Here's a minimal reproducible example (click the button, which throws an exception, but it's not caught as expected):</p>
  <pre><code>import 'dart:async';
  import 'package:flutter/material.dart';
  
  void main() =&gt;
    runZoned&lt;Future&lt;void&gt;&gt;(
      () async {
        runApp(MyApp());
      },
      onError: (dynamic error, StackTrace stackTrace) {
        print(&quot;=================== CAUGHT DART ERROR&quot;);
        // Send report
        // NEVER REACHES HERE - WHY?
      },
    );
  
  class MyApp extends StatefulWidget {
    // This widget is the root of your application.
    @override
    _MyAppState createState() =&gt; _MyAppState();
  }
  
  class _MyAppState extends State&lt;MyApp&gt; {
  
    @override
    void initState() {
      super.initState();
  
      // This captures errors reported by the FLUTTER framework.
      FlutterError.onError = (FlutterErrorDetails details) {
        print(&quot;=================== CAUGHT FLUTTER ERROR&quot;);
        // Send report
        // NEVER REACHES HERE - WHY?
      };
    }
  
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Scaffold(
          body: SafeArea(
            child: RaisedButton(
              child: Text(&quot;Throw exception&quot;),
              onPressed: () {
                throw Exception(&quot;This is a test exception&quot;);
              },
            ),
          ),
        ),
      );
    }
  }
  </code></pre>