Skip to main content

Dart's catch syntax revisited

Posted by Gilad Bracha

We've decided to clean up the syntax of the catch clauses. The current syntax is the one place where a type annotation (or at least, what looks like a type annotation) has semantic meaning. To reduce confusion, it is desirable to make this construct be syntactically distinct.

After discussion we concluded that the the syntax would be:

on T catch (e, s) {...}

where the catch clause is optional; it may be omitted if the variables are not needed. The stacktrace variable is optional as well. Both variables are implicitly final and implicitly typed. The type of e is T and the type of s is StackTrace (an interface yet to be defined).

We also want to support a version where the on clause is elided but the catch clause is retained, which can be used as a catch-all, and is also javascript compatible:

catch (e) {...}

Spec Changes

The required spec changes are given below, with new/changed (excluding deletions) highlighted in yellow below. I’m pleased to note that the description is now slightly simpler.


The try statement supports the definition of exception handling code in a structured way.

tryStatement:      try block (onPart+ finallyPart? | finallyPart)

onPart:      catchPart block
   | on qualified catchPart? block

catchPart:      catch '(' identifier (',' identifier)? ')'     ;finallyPart:      finally block    ;
A try statement consists of a block statement, followed by at least one of:
  1. A set of on-catch clauses, each of which specifies (either explicitly or implicitly) the type of exception object to be handled, one or two exception parameters and a block statement.
  2. A finally clause, which consists of a block statement.

An on-catch clause of the form on T catch (p1, p2) s or on T s matches an object o if the type of o is a subtype of T.  It is a compile-time error if T does not denote a type available in the lexical scope of the catch clause.

An on-catch clause of the form on T catch (p1) s, is equivalent to an on-catch clause on T catch (p1, p2) s, where p2 is an identifier that does not occur anywhere else in the program.   

An on-catch clause of the form catch (p) s is equivalent to an an on-catch clause on Object catch (p) s. An on-catch clause of the form catch (p1, p2) s is equivalent to an an on-catch clause on Object catch (p1, p2) s.

The definition below is an attempt to characterize exception handling without resorting to a normal/abrupt completion formulation. It has the advantage that one need not specify abrupt completion behavior for every compound statement.  On the other hand, it is new different and needs more thought.

A try statement try s1 on-catch1 ... on-catchn finally sf  defines an exception handler h that executes as follows:

The on-catch clauses are examined in order, starting with on-catch1, until either an on-catch clause that matches the current exception is found, or the list of on-catch clauses has been exhausted. If an on-catch clause on-catchk is found, it is executed. If no on-catch clause is found, the finally clause is executed. Then, execution resumes at the end of the try statement.

A finally clause finally s defines an exception handler h that executes by executing the finally clause. Then, execution resumes at the end of the try statement.

Execution of an on-catch clause on T catch (p1, p2) s of a try statement t proceeds as follows:

Variables p1 of static type T, and p2 of static type StackTrace are implicitly declared, with a scope comprising s.  The variable p1 is bound to the current exception, and p2 is bound to the current stack trace. Next, the statement s is executed in the dynamic scope of the exception handler defined by the finally clause of t. Then, the current exception and current stack trace both become undefined.

Execution of a finally clause finally s of a try statement proceeds as follows:

The statement s is executed. Then, if the current exception is defined, control is transferred to the nearest dynamically enclosing exception handler.

Execution of a try statement of the form try s1 on-catch1 ... on-catchn finally sf  proceeds as follows:

The statement s1 is executed in the dynamic scope of the exception handler defined by the try statement. Then, the finally clause is executed.

Whether any of the on-catch clauses is executed depends on whether a matching exception has been raised by s1 (see the specification of the throw statement).

If s1 has raised an exception, it will transfer control to the try statement’s handler, which will examine the on-catch clauses in order for a match as specified above. If no matches are found, the handler will execute the finally clause.

If a matching handler was found, it will execute first, and then the finally clause will be executed.

If an exception is raised during execution of a on-catch clause, this will transfer control to the handler for the finally clause, causing the finally clause to execute in this case as well.

If no exception was raised, the finally clause is also executed. Execution of the finally clause could also raise an exception, which will cause transfer of control to the next enclosing handler.

Popular posts from this blog

A stronger Dart for everyone

We are constantly asking ourselves:
How do we make developers even more productive when writing Dart apps? We believe that a critical part of the answer to this question is to make strongmode – a sound static type system for Dart – the standard for all Dart developers.

Teams that use Dart to build apps like Soundtrap, AdWords, AdSense, and Greentea say they really enjoy using strong mode features, such as early error detection. In fact, teams that have switched completely to strong mode cite not only early error detection but also better code readability and maintainability as major benefits. We hear this both from small teams and Рeven more so Рfrom large teams with hundreds of developers writing and maintaining millions of lines of Dart code. As Björn Sperber from Soundtrap says,
Strong mode and the smooth integration with IntelliJ is a joy to use and a huge improvement. If you’ve tried out Flutter, you’ve already used strong mode checks from the Dart analyzer.

Given the benefits …

AngularDart 3.0: Easy upgrade, better performance

AngularDart 3.0 is now available. It brings better performance and smaller generated code, while also making you more productive.

Version 3.0 is an evolution: although it has some breaking changes (detailed below), it is a smooth upgrade due to minimal public API adjustments. Most of the progress is under the hood—in code quality, stability, generated code size, performance, and developer experience.

Code quality:
2731 instances of making the framework code more type safe (using sound Dart).The AngularDart framework code size is down by 12%.Many additional style updates to the codebase:Changed to use idiomatic <T> for generic methods.Removed NgZoneImpl, all the code exists in NgZone now.Stability:
Many CSS encapsulation fixes due to update with csslib package.Fixed bugs with IE11.

For the Mail sample app, we see 30% faster time-to-interactive (currently 3812 ms on a simulated 3G connection, measured via Lighthouse).Our large app benchmark shows 2x faster render times of…

AngularDart 4

AngularDart v4 is now available. We've been busy since the release angular2 v3.1.0 in May. Not only did we "drop the 2", but we also improved the compiler and tightened up the framework to give you smaller code, we updated the package structure to improve usability, and we added several new features. Check out the updated documentation to get started.
Just angular Upgrading to v4 will require more than updating your version constraint. The package has changed names (back) to angular – dropping the 2. You'll need to update your pubspec.yaml and the corresponding imports in your code. In most instances, find-and-replace should do the trick. Going forward, the package will be called package:angular. We'll just update the version number.
Smaller code The updated compiler in 4.0 allows type-based optimizations that not only improve runtime performance but generate better code because we are able to strongly type templates. A big result of the update is that many ap…