1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>AARM95 - Exception Handlers</TITLE>
<META NAME="Author" CONTENT="JTC1/SC22/WG9/ARG, by Randall Brukardt, ARG Editor">
<META NAME="GENERATOR" CONTENT="Arm_Form.Exe, Ada Reference Manual generator">
<STYLE type="text/css">
DIV.paranum {position: absolute; font-family: Arial, Helvetica, sans-serif; left: 0.5 em; top: auto}
TT {font-family: "Courier New", monospace}
DT {display: compact}
DIV.Normal {font-family: "Times New Roman", Times, serif; margin-bottom: 0.6em}
DIV.Wide {font-family: "Times New Roman", Times, serif; margin-top: 0.6em; margin-bottom: 0.6em}
DIV.Annotations {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-bottom: 0.6em}
DIV.WideAnnotations {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-top: 0.6em; margin-bottom: 0.6em}
DIV.Index {font-family: "Times New Roman", Times, serif}
DIV.SyntaxSummary {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-bottom: 0.4em}
DIV.Notes {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-bottom: 0.6em}
DIV.NotesHeader {font-family: "Times New Roman", Times, serif; margin-left: 2.0em}
DIV.SyntaxIndented {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-bottom: 0.4em}
DIV.Indented {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-bottom: 0.6em}
DIV.CodeIndented {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-bottom: 0.6em}
DIV.SmallIndented {font-family: "Times New Roman", Times, serif; margin-left: 10.0em; margin-bottom: 0.6em}
DIV.SmallCodeIndented {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-bottom: 0.6em}
DIV.Examples {font-family: "Courier New", monospace; margin-left: 2.0em; margin-bottom: 0.6em}
DIV.SmallExamples {font-family: "Courier New", monospace; font-size: 80%; margin-left: 7.5em; margin-bottom: 0.6em}
DIV.IndentedExamples {font-family: "Courier New", monospace; margin-left: 8.0em; margin-bottom: 0.6em}
DIV.SmallIndentedExamples {font-family: "Courier New", monospace; font-size: 80%; margin-left: 15.0em; margin-bottom: 0.6em}
UL.Bulleted {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-right: 2.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.SmallBulleted {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.NestedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-right: 4.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.SmallNestedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-right: 8.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.IndentedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-right: 8.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.CodeIndentedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.CodeIndentedNestedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-right: 8.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.SyntaxIndentedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-right: 4.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.NotesBulleted {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-right: 4.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.NotesNestedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
DL.Hanging {font-family: "Times New Roman", Times, serif; margin-top: 0em; margin-bottom: 0.6em}
DD.Hanging {margin-left: 6.0em}
DL.IndentedHanging {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-top: 0em; margin-bottom: 0.6em}
DD.IndentedHanging {margin-left: 2.0em}
DL.HangingInBulleted {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-right: 2.0em; margin-top: 0em; margin-bottom: 0.5em}
DD.HangingInBulleted {margin-left: 4.0em}
DL.SmallHanging {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-top: 0em; margin-bottom: 0.6em}
DD.SmallHanging {margin-left: 7.5em}
DL.SmallIndentedHanging {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-top: 0em; margin-bottom: 0.6em}
DD.SmallIndentedHanging {margin-left: 2.0em}
DL.SmallHangingInBulleted {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
DD.SmallHangingInBulleted {margin-left: 5.0em}
DL.Enumerated {font-family: "Times New Roman", Times, serif; margin-right: 0.0em; margin-top: 0em; margin-bottom: 0.5em}
DD.Enumerated {margin-left: 2.0em}
DL.SmallEnumerated {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-right: 4.0em; margin-top: 0em; margin-bottom: 0.5em}
DD.SmallEnumerated {margin-left: 2.5em}
DL.NestedEnumerated {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-right: 2.0em; margin-top: 0em; margin-bottom: 0.5em}
DL.SmallNestedEnumerated {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
</STYLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFF0" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000">
<P><A HREF="AA-TOC.html">Contents</A> <A HREF="AA-0-29.html">Index</A> <A HREF="AA-11-1.html">Previous</A> <A HREF="AA-11-3.html">Next</A></P>
<HR>
<H1> 11.2 Exception Handlers</H1>
<DIV Class="Paranum"><FONT SIZE=-2>1</FONT></DIV>
<DIV Class="Normal"> [The response to one or more exceptions is specified
by an <FONT FACE="Arial, Helvetica">exception_handler</FONT>.] </DIV>
<H4 ALIGN=CENTER>Syntax</H4>
<DIV Class="Paranum"><FONT SIZE=-2>2</FONT></DIV>
<DIV Class="SyntaxIndented"><FONT FACE="Arial, Helvetica">handled_sequence_of_statements<A NAME="I4036"></A>
::= </FONT><BR>
<A NAME="I4037"></A><FONT FACE="Arial, Helvetica">sequence_of_statements</FONT><BR>
[<B>exception</B><BR>
<A NAME="I4038"></A><FONT FACE="Arial, Helvetica">exception_handler</FONT><BR>
{<A NAME="I4039"></A><FONT FACE="Arial, Helvetica">exception_handler</FONT>}]</DIV>
<DIV Class="Paranum"><FONT SIZE=-2>3</FONT></DIV>
<DIV Class="SyntaxIndented"><FONT FACE="Arial, Helvetica">exception_handler<A NAME="I4040"></A>
::= </FONT><BR>
<B>when</B> [<A NAME="I4041"></A><FONT FACE="Arial, Helvetica">choice_parameter_specification</FONT>:] <A NAME="I4042"></A><FONT FACE="Arial, Helvetica">exception_choice</FONT> {| <A NAME="I4043"></A><FONT FACE="Arial, Helvetica">exception_choice</FONT>} =><BR>
<A NAME="I4044"></A><FONT FACE="Arial, Helvetica">sequence_of_statements</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>4</FONT></DIV>
<DIV Class="SyntaxIndented"><FONT FACE="Arial, Helvetica">choice_parameter_specification<A NAME="I4045"></A>
::= </FONT><A NAME="I4046"></A><FONT FACE="Arial, Helvetica">defining_identifier</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>5</FONT></DIV>
<DIV Class="SyntaxIndented"><FONT FACE="Arial, Helvetica">exception_choice<A NAME="I4047"></A>
::= </FONT><I>exception_</I><A NAME="I4048"></A><FONT FACE="Arial, Helvetica">name</FONT> | <B>others</B></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>5.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>To be honest: </B><A NAME="I4049"></A>``<I>Handler</I>''
is an abbreviation for ``<FONT FACE="Arial, Helvetica">exception_handler</FONT>.''</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>5.b</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><A NAME="I4050"></A>Within this
section, we sometimes abbreviate ``<FONT FACE="Arial, Helvetica">exception_choice</FONT>''
to ``<I>choice</I>.'' </FONT></DIV>
<H4 ALIGN=CENTER>Legality Rules</H4>
<DIV Class="Paranum"><FONT SIZE=-2>6</FONT></DIV>
<DIV Class="Normal"> <A NAME="I4051"></A>A choice with an <I>exception_</I><FONT FACE="Arial, Helvetica">name</FONT>
<I>covers</I> the named exception. A choice with <B>others</B> covers
all exceptions not named by previous choices of the same <FONT FACE="Arial, Helvetica">handled_sequence_of_statements</FONT>.
Two choices in different <FONT FACE="Arial, Helvetica">exception_handler</FONT>s
of the same <FONT FACE="Arial, Helvetica">handled_sequence_of_statements</FONT>
shall not cover the same exception. </DIV>
<DIV Class="Paranum"><FONT SIZE=-2>6.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Ramification: </B>Two <FONT FACE="Arial, Helvetica">choice</FONT>s
of the same <FONT FACE="Arial, Helvetica">exception_handler</FONT> may
cover the same exception. For example, given two renaming declarations
in separate packages for the same exception, one may nevertheless write,
for example, ``<B>when</B> Ada.Text_IO.Data_Error | My_Seq_IO.Data_Error
=>''.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>6.b</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>An <B>others</B> choice even covers
exceptions that are not visible at the place of the handler. Since exception
raising is a dynamic activity, it is entirely possible for an <B>others</B>
handler to handle an exception that it could not have named. </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>7</FONT></DIV>
<DIV Class="Normal"> A choice with <B>others</B> is allowed only for
the last handler of a <FONT FACE="Arial, Helvetica">handled_sequence_of_statements</FONT>
and as the only choice of that handler.</DIV>
<DIV Class="Paranum"><FONT SIZE=-2>8</FONT></DIV>
<DIV Class="Normal"> An <I>exception_</I><FONT FACE="Arial, Helvetica">name</FONT>
of a choice shall not denote an exception declared in a generic formal
package. </DIV>
<DIV Class="Paranum"><FONT SIZE=-2>8.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Reason: </B>This is because
the compiler doesn't know the identity of such an exception, and thus
can't enforce the coverage rules. </FONT></DIV>
<H4 ALIGN=CENTER>Static Semantics</H4>
<DIV Class="Paranum"><FONT SIZE=-2>9</FONT></DIV>
<DIV Class="Normal"> <A NAME="I4052"></A>A <FONT FACE="Arial, Helvetica">choice_parameter_specification</FONT>
declares a <I>choice parameter</I>, which is a constant object of type
Exception_Occurrence (see <A HREF="AA-11-4-1.html">11.4.1</A>). During
the handling of an exception occurrence, the choice parameter, if any,
of the handler represents the exception occurrence that is being handled.</DIV>
<H4 ALIGN=CENTER>Dynamic Semantics</H4>
<DIV Class="Paranum"><FONT SIZE=-2>10</FONT></DIV>
<DIV Class="Normal"> <A NAME="I4053"></A>The execution of a <FONT FACE="Arial, Helvetica">handled_sequence_of_statements</FONT>
consists of the execution of the <FONT FACE="Arial, Helvetica">sequence_of_statements</FONT>.
[The optional handlers are used to handle any exceptions that are propagated
by the <FONT FACE="Arial, Helvetica">sequence_of_statement</FONT>s.]
</DIV>
<H4 ALIGN=CENTER>Examples</H4>
<DIV Class="Paranum"><FONT SIZE=-2>11</FONT></DIV>
<DIV Class="Normal" Style="margin-bottom: 0.4em"> <I>Example of an
exception handler:</I> </DIV>
<DIV Class="Paranum"><FONT SIZE=-2>12</FONT></DIV>
<DIV Class="Examples"><TT><B>begin</B><BR>
Open(File, In_File, "input.txt"); <I>-- see <A HREF="AA-A-8-2.html">A.8.2</A></I><BR>
<B>exception</B><BR>
<B>when</B> E : Name_Error =><BR>
Put("Cannot open input file : ");<BR>
Put_Line(Exception_Message(E)); <I>-- see <A HREF="AA-11-4-1.html">11.4.1</A></I><BR>
<B>raise</B>;<BR>
<B>end</B>;</TT></DIV>
<H4 ALIGN=CENTER>Extensions to Ada 83</H4>
<DIV Class="Paranum"><FONT SIZE=-2>12.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><A NAME="I4054"></A>The syntax
rule for <FONT FACE="Arial, Helvetica">exception_handler</FONT> is modified
to allow a <FONT FACE="Arial, Helvetica">choice_parameter_specification</FONT>.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>12.b</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>Different <FONT FACE="Arial, Helvetica">choice</FONT>s
of the same <FONT FACE="Arial, Helvetica">exception_handler</FONT> may
cover the same exception. This allows for ``when Numeric_Error | Constraint_Error
=>'' even though Numeric_Error is a rename of Constraint_Error. This
also allows one to ``with'' two different I/O packages, and then write,
for example, ``when Ada.Text_IO.Data_Error | My_Seq_IO.Data_Error =>''
even though these might both be renames of the same exception. </FONT></DIV>
<H4 ALIGN=CENTER>Wording Changes from Ada 83</H4>
<DIV Class="Paranum"><FONT SIZE=-2>12.c</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>The syntax rule for <FONT FACE="Arial, Helvetica">handled_sequence_of_statements</FONT>
is new. These are now used in all the places where handlers are allowed.
This obviates the need to explain (in Sections 5, 6, 7, and 9) what portions
of the program are handled by the handlers. Note that there are more
such cases in Ada 95.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>12.d</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>The syntax rule for <FONT FACE="Arial, Helvetica">choice_parameter_specification</FONT>
is new. </FONT></DIV>
<HR>
<P><A HREF="AA-TOC.html">Contents</A> <A HREF="AA-0-29.html">Index</A> <A HREF="AA-11-1.html">Previous</A> <A HREF="AA-11-3.html">Next</A> <A HREF="AA-TTL.html">Legal</A></P>
</BODY>
</HTML>
|