Skip to main content

New site for Dart news and articles

For the latest Dart news, visit our new blog at  https://medium.com/dartlang .

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.

Try


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.