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.

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.

Popular posts from this blog

Dart in 2016: The fastest growing programming language at Google, 2nd fastest growing in TIOBE Index

Dart was the fastest growing programming language at Google in 2016 with millions of lines of code written. It also made it to TIOBE Index Top 20 this month (see TIOBE's methodology).

It takes time to build something as ambitious as Dart and, in some ways, Dart is still in its infancy. But we're glad the hard work is starting to pay off.

Many thanks to our amazing community!

We're going to celebrate by ... releasing 1.22 next week (as per our usual 6 week release schedule).

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.

Performance:
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…