Section 10.2 - Declaring Exceptions

Before you can raise or handle an exception, it must be declared. Declaring exceptions is just like declaring a variable of type exception; here's an example:

  Singularity : exception;

To be complete, here's the syntax for defining an exception, describing using BNF:

  exception_declaration ::= defining_identifier_list ": exception;"
  defining_identifier_list ::= identifier { "," identifier }

Exception declarations are generally placed in a package declaration.

Raising an exception is easy, too - just use the raise statement. A raise statement is simply the keyword "raise" followed by the name of the exception; the syntax in BNF is:

  raise_statement ::= "raise" [ exception_name ] ";"

You'll notice that the exception_name is optional; we'll discuss what that means in the next section.


There is no quiz question for this section.

You may go to the next section.


You may also:

PREVIOUS Go back to the previous section

OUTLINE  Go up to the outline of lesson 10

David A. Wheeler (wheeler@ida.org)