The purpose of function `runZoned` of 'dart:async' The purpose of function `runZoned` of 'dart:async' dart dart

The purpose of function `runZoned` of 'dart:async'


Look at this code:

import 'dart:async';void main() {  fineMethod().catchError((s) {}, test : (e) => e is String);  badMethod().catchError((s) {}, test : (e) => e is String);}Future fineMethod() {  return new Future(() => throw "I am fine");}Future badMethod() {  new Future(() => throw "I am bad");  return new Future(() => throw "I am fine");}

Output

Unhandled exception:I am bad

Now look at this code:

import 'dart:async';void main() {  fineMethod().catchError((s) {}, test : (e) => e is String);  runZoned(() {    badMethod().catchError((s) {}, test : (e) => e is String);  }, onError : (s) {    print("It's not so bad but good in this also not so big.");    print("Problem still exists: $s");  });}Future fineMethod() {  return new Future(() => throw "I am fine");}Future badMethod() {  new Future(() => throw "I am bad");  return new Future(() => throw "I am fine");}

Output

It's not so bad but good in this also not so big.Problem still exists: I am bad

You should strictly avoid using badMethod if this possible.

Only if this not possible you may temporary use runZoned

Also you may use runZoned to simulate sandboxed execution of tasks.