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. For example, to raise the "Singularity" exception defined above, just say:

  raise Singularity;

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.


Quiz:

Which of the following would define an exception named No_Safety_Net?

  1. No_Safety_Net : exception;
  2. Exception : No_Safety_Net;

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 10 outline

David A. Wheeler (dwheeler@dwheeler.com)

The master copy of this file is at "http://www.adahome.com/Tutorials/Lovelace/s10s2.htm".