Note: information on this page refers to Ceylon 1.1, not to the current release.

throw statement

The throw statement is used to raise an exception

Usage

A bare throw doesn't need to supply an exception instance:

throw;

An exception instance may be explicitly specified. Usually, a new exception is instantiated:

throw Exception();

Description

Execution

The throw statement initiates the propogation of an exception, unwinding the call stack. The call stack is searched for the nearest try statement with a matching catch clause, and execution resumes at the start of that catch block (possibly after resource cleanup.

A method, getter, setter, or initializer, is said to return abnormally if an exception propgates beyond its scope. This signifies an exceptional circumstance which prevents normal completion of the method, getter, setter or initializer.

An expression may be given. If no expression is given a new messageless and causeless instance of Exception is created automatically. If an expression is given is must be assignable to Exception.

Notes

  • Ceylon does not support 'checked' exceptions. Any kind of exception may be thrown without it having to be declared by a throws annotation. This is even the case when Ceylon code throws what in Java would be considered a checked exception (such as java.lang.Exception). In other words the following is perfectly acceptable to the Ceylon compiler:
  import java.lang { CheckedException=Exception }

  void m() {
      throw CheckedException();
  }

Advice

Exceptions are used somewhat less in Ceylon than in other languages, since null or [] may be used to indicate certain kinds of problems in a more typesafe way. For example, list[-1] evaluates to null in Ceylon, instead of throwing an exception.

It is possible, though not usually recommended, to use throw to implement control logic. Used in this manner, throw is a sort of poor-man's goto.

See also