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

Const, Static, Final, Oh my!

Posted by Seth Ladd

(This is an "oldie but a goodie" post originally written by Bob Nystrom. It is being posted here as the explanations still ring true.)

Bob writes:

"static", "final", and "const" mean entirely distinct things in Dart:

"static" means a member is available on the class itself instead of on instances of the class. That's all it means, and it isn't used for anything else. static modifies *members*.

"final" means single-assignment: a final variable or field *must* have an initializer. Once assigned a value, a final variable's value cannot be changed. final modifies *variables*.

"const" has a meaning that's a bit more complex and subtle in Dart. const modifies *values*. You can use it when creating collections, like const [1, 2, 3], and when constructing objects (instead of new) like const Point(2, 3). Here, const means that the object's entire deep state can be determ…

The new AdWords UI uses Dart — we asked why

Google just announced a re-designed AdWords experience. In case you’re not familiar with AdWords: businesses use it to advertise on and partner websites. Advertising makes up majority of Google’s revenue, so when Google decides to completely redo the customer-facing front end to it, it’s a big deal. The Dart team is proud to say that this new front end is built with Dart and Angular 2. Whenever you asked us whether Google is ‘even using Dart for anything,’ this is what we had in mind but couldn’t say aloud. Until now. We asked Joshy Joseph, the primary technical lead on the project, some questions. Joshy is focusing on things like infrastructure, application latency and development velocity, so he’s the right person to ask about Dart.Q: What exactly did we launch on Monday?It’s a complete redesign of the AdWords customer experience that is rolling out slowly as a test to a small initial set of advertisers. The most noticeable thing is probably the Material Design look and f…

Dart 1.12 Released, with Null-Aware Operators and more

Dart 1.12.0 is now released! It contains the new null-aware operators language feature, and enhancements to pub, Observatory, dartdoc, and much more.

Null-aware operators

The new null-aware operators help you reduce the amount of code required to work with references that are potentially null. This feature is a collection of syntactic sugar for traversing (potentially null) object calls, conditionally setting a variable, and evaluating two (potentially null) expressions.

Click or tap the red Run button below to see them in action.


  if null operator. `expr1 ?? expr2` evaluates to `expr1` if not `null`, otherwise `expr2`.


  null-aware assignment. `v ??= expr` causes `v` to be assigned `expr` only if `v` is `null`.


  null-aware access. `x?.p` evaluates to `x.p` if `x` is not `null`, otherwise evaluates to `null`.


  null-aware method invocation. `x?.m()` invokes `m` only if `x` is not `null`.

Learn more about Dart's null-aware operators in our Language Tour.

.packages fi…