Zones
Often, a program generates an uncaught exception and terminates the execution. The commonly occurring exceptions in the program means that the code is broken and must be fixed. However, sometimes exceptions may happen due to errors in communication, hardware faults, and so on. The following is an example of the HTTP server, which is used to demonstrate this problem:
import 'dart:io'; main() { runServer(); } runServer() { HttpServer .bind(InternetAddress.ANY_IP_V4, 8080) .then((server) { server.listen((HttpRequest request) { request.response.write('Hello, world!'); request.response.close(); }); }); }
The code in the main
function can be terminated due to uncaught errors that may happen in the runServer
function. Termination of the program under those circumstances can be undesirable.
So, how can this problem be solved? We wrap our code within a try/catch block to catch all the uncaught exceptions and it works perfectly, as shown in...