# Copyright (C) 2001-2010, Parrot Foundation.

=head1 PDD 23: Exceptions

=head2 Abstract

This document defines the requirements and implementation strategy for
Parrot's exception system.

=head2 Description

I<Exceptions> are indications by running code that something unusual -- an
"exception" to the normal processing -- has occurred.  When code detects an
exceptional condition, it I<throws> an exception object.  Before this occurs,
code can register exception I<handlers>, which are functions (or closures)
which may (but are not obligated to) I<handle> the exception.  Some exceptions
permit continued execution immediately after the I<throw>; some don't.

Exceptions transfer control to a piece of code outside the normal flow of
control.  They are mainly used for error reporting or cleanup tasks.

(A digression on terminology: In a system analysis sense, the word "exception"
usually refers to the exceptional event that requires out-of-band handling.
However, in Parrot, "exception" also refers to the object that holds all the
information describing the exceptional condition: the nature of the exception,
the error message describing it, and other ancillary information.  The
specific type (class) of an exception object indicates its category.)

=head3 Exception Opcodes

These are the opcodes relevant to exceptions and exception handlers:

=over

=item B<push_eh I<LABEL>>

=item B<push_eh I<EXCEPTIONHANDLER_PMC>>

Push an exception handler pmc onto the exception handler stack.

When an exception is thrown, Parrot walks up the stack of active exception
handlers, invoking each one in turn, but still in the dynamic context of the
exception (i.e. the call stack is I<not> unwound first).  See below for more
detail.

If a I<LABEL> is provided, Parrot creates and pushes an exception handler
that resumes execution at I<LABEL> if invoked, which has the effect of
unconditionally handling all errors, and unwinding the stack to that label.

If an I<EXCEPTIONHANDLER_PMC> is provided, Parrot pushes that pmc itself
onto the exception handler stack.

=item B<pop_eh>

Pop the most recently pushed exception handler off the exception handler
stack.

=item B<throw I<EXCEPTION> [ , I<CONTINUATION> ]>

Throw an exception consisting of the given I<EXCEPTION> PMC, after taking a
continuation at the next opcode. When a I<CONTINUATION> is passed in, it will
use that instead of generating a new continuation. Active exception handlers
(if any) will be invoked with I<EXCEPTION> as the only parameter. The
I<CONTINUATION> is stored in the 'resume' slot of the I<EXCEPTION>.

PMCs other than Parrot's Exception PMC may also be thrown, but they must
support the interface of an Exception PMC. An HLL may implement throwing any
arbitrary type of PMC, by storing that PMC in the 'payload' slot of the
Exception PMC.

Exception handlers can resume execution after handling the exception by
invoking the continuation stored in the 'resume' slot of the exception object.
That continuation must be invoked with no parameters; in other words, C<throw>
never returns a value.

=item B<rethrow I<EXCEPTION>>

While handling an exception, rethrow the exception to the next handler. Aside
from selecting a different handler, the behaviour of C<rethrow> is the same as
C<throw>. Each successive call to C<rethrow> will select a different handler,
until it exhausts the list of possible handlers. A rethrown exception that
is not handled behaves the same as an unhandled C<throw>n exception.

=item B<die [ I<MESSAGE> ]>

The C<die> opcode throws an exception of type C<exception;death> and severity
C<EXCEPT_error> with a payload of I<MESSAGE>.  The exception payload is a
C<String> PMC containing I<MESSAGE>.

{{NOTE: Exception classes NYI.  Currently throws CONTROL_ERROR}}

The default when no I<MESSAGE> is given is "Fatal exception at LINE in
FILE." followed by a backtrace.

{{NOTE: Not yet implemented.}}

If this exception is not handled, it results in Parrot returning an error
indication and the stringification of I<MESSAGE> to its embedding environment.
When running standalone, this means writing the stringification of I<MESSAGE>
to standard error and executing the standard Parrot function C<Parrot_x_exit>,
to shut down the interpreter cleanly.

=item B<exit [ I<EXITCODE> ]>

Throw an exception of type C<exception;exit> with a payload of I<EXITCODE>,
which defaults to zero, as an Integer PMC.

{{NOTE: Exception classes NYI. Currently throws a type based on the
EXITCODE.}}

If not handled, this exception results in Parrot returning I<EXITCODE>
as a status to its embedded environment, or when running standalone,
to execute the C function C<exit(I<EXITCODE>)>.

{{NOTE: This is not currently the case.  Parrot now stores the EXITCODE
argument in the type, not the payload}}

=back

=head3 Exception Introspection Opcodes

These are the opcodes relevant to introspection of the exception handler
stack:

=over

=item B<count_eh>

Return the quantity of currently active exception handlers.

=back


=head3 Order of Operations in Exception Handling

When B<throw> is called, for all active exception handlers, in LIFO order:

=over

=item 1
Find the topmost exception handler.

=item 2
Push an exception record somewhere, presumably on the exception handler
stack. The exception record contains a pointer to an exception handler
block, an exception PMC, and (optionally) a continuation.

=item 3
Invoke the handler (note: this is still in the thrower's dynamic
context).

=item 4
If the exception is C<rethrow>n, repeat steps 1-3 above, finding the next
exception handler.

=item 5
If no handler is found, and the exception is non-fatal (such as a
warning), and there is a continuation in the exception record (because
the throwing opcode was C<throw>), invoke the continuation (resume
execution). Whether to resume or die when an exception isn't handled is
determined by the severity of the exception.

=item 6
Otherwise terminate the program like C<die>.

=back

When running an embedded Parrot interpreter, the interpreter does not
immediately terminate on an unhandled exception, it merely returns
control to the embedding program and stores the unhandled exception so
that it may be queried by the embedding program. The embedding program
may choose to handle the exception and continue execution by invoking
the exception's continuation.

=head2 Implementation

=head3 Exception Object Interface

All of Parrot's standard exceptions provide at least the following interface.
It is recommended that all classes intended for throwing also provide at least
this interface as well.

=over 4

=item B<PMC *get_attr_str(STRING *name)>

Retrieve an attribute from the Exception. All exceptions will have at least
C<message>, C<severity>, C<resume>, and C<payload> attributes.

The C<message> is an exception's human-readable self-description.  Note that
the type of the returned PMC isn't required to be C<String>, but you should
still be able to stringify and print it.

The C<severity> is an integer from an internal Parrot enum of exception
severities.

The C<resume> is a continuation that you can invoke to resume normal execution
of the program.

The C<payload> more specifically identifies the detailed cause/nature of
the exception.  Each exception class will have its own specific payload
type(s).  See the table of standard exception classes for examples.

=item B<PMC *set_attr_str(STRING *name, PMC *value)>

Set an attribute on the Exception. All exceptions will have at least
C<message>, C<severity>, C<resume>, and C<payload> attributes.

=item B<PMC *annotations()>

Gets a Hash containing any bytecode annotations in effect at the point where
the exception was thrown. If none were in effect, returns an empty Hash. See
the PIR PDD for syntax for declaring and semantics of bytecode annotations.

=item B<PMC *annotations(STRING *name)>

Returns a PMC representing the bytecode annotation with the key specified in
C<name> at the point where the exception was thrown. If there was no such
annotation in effect, a NULL PMC will be returned.

=item B<PMC *backtrace()>

Gets a representation of the backtrace at the point that this exception was
thrown. Returns an array of hashes. Each array element represents a caller in
the backtrace, the most recent caller first. The hash has two keys: C<sub>,
which holds the PMC representing the sub, and C<annotations> which is a hash
of the annotations at the point where the exception was thrown for the current
sub, or for the point of the call a level deeper for the rest.

=back

=head3 Standard Parrot Exceptions

Parrot comes with a small hierarchy of classes designed for use as exceptions.
Parrot throws them when internal Parrot errors occur, but any user code can
throw them too.

{{NOTE: Currently NYI.  Parrot currently uses integers to represent exception
types.}}

{{NOTE: Questions about how this interoperates with custom HLL exception
classes}}

=over

=item B<exception>

Base class of all standard exceptions.  Provides no special functionality.
Exists for the purpose of C<isa> testing.

=item B<exception;death>

Exception type that is thrown by the C<die> opcode. See the description of
the C<die> opcode in this document.

=item B<exception;errno>

A system error as reported in the C variable C<errno>.  Payload is an integer.
Message is the return value of the standard C function C<strerror()>.

=item B<exception;exit>

Exception type that is thrown by the C<exit> opcode. See the description of
the C<exit> opcode in this document.

=item B<exception;math>

Generic base class for math errors.

=item B<exception;math;division_by_zero>

Division by zero (integer or float).  No payload.

=item B<exception;domain>

Generic base class for miscellaneous domain (input value) errors.  Payload is
an array, the first element of which is the operation that failed (e.g. the
opcode name); subsequent elements depend on the value of the first element.

(Note: There is not a separate exception class for every operation that might
throw a domain exception.  Class proliferation is expensive, both to Parrot
and to the humans working with it who have to memorize a class hierarchy.  But
I understand the temptation.)

=item B<exception;lexical>

An C<find_lex> or C<store_lex> operation failed because a given lexical
variable was not found.  Payload is an array: [0] the name of the lexical
variable that was not found, [1] the LexPad in which it was not found.

=back

=head3 Opcodes that Throw Exceptions

Exceptions have been incorporated into built-in opcodes in a limited way.  For
the most part, they're used when the return value is either impractical to
check (perhaps because we don't want to add that many error checks in line),
or where the output type is unable to represent an error state (e.g. the
output I register of the C<ord> opcode).

The C<div>, C<fdiv>, and C<cmod> opcodes throw
C<exception;math;division_by_zero>.

The C<ord> opcode throws C<exception;domain> when it's passed an empty
argument or a string index that's outside the length of the string.  Payload
is an array, first element being the string 'ord'.

The C<find_encoding> opcode throws C<exception;domain> if the encoding name
it's looking up doesn't exist.  Payload is an array: [0] string
'find_encoding', [1] encoding name that was not found.

The C<trans_encoding> opcode throws C<exception;domain> on "information loss"
(presumably, this means when one encoding doesn't have a one-to-one
correspondence in the other encoding).  Payload is an array: [0] string
'trans_encoding', [1] source encoding name, [2] destination encoding name, [3]
untranslatable code point.

Parrot's default version of the C<LexPad> PMC throws C<exception;lexical> for
some error conditions, though other implementations can choose to return error
values instead.

By default, the C<find_lex> and C<store_lex> opcodes throw an exception
(C<exception;lexical>) when the given name can't be found in any visible
lexical pads.  However, this behavior is only a default, as provided by the
default Parrot lexical pad PMC C<LexPad>.  If a given HLL has its own lexical
pad PMC, its behavior may be very different.  (For example, in Tcl,
C<store_lex> is likely to succeed every time, as creating new lexicals at
runtime is OK in Tcl.)

{{ TODO: List any other opcodes that currently throw exceptions and
general categories of opcodes that should throw exceptions. }}

Other opcodes respond to an C<errorson> setting to decide whether to
throw an exception or return an error value. C<get_hll_global> and
C<get_root_global> throw an
exception (or returns a Null PMC) if the global name requested doesn't
exist. C<find_name> throws an exception (or returns a Null PMC) if the
name requested doesn't exist in a lexical, current, global, or built-in
namespace.

{{ TODO: "errorson" as specified is dynamically rather than lexically
scoped; is this good? Probably not good. Let's revisit it when we get
the basic exceptions functionality implemented. }}

It's a little odd that so few opcodes throw exceptions (these are the
ones that are documented, but a few others throw exceptions internally
even though they aren't documented as doing so). It's worth considering
either expanding the use of exceptions consistently throughout the
opcode set, or eliminating exceptions from the opcode set entirely. The
strategy for error handling should be consistent, whatever it is. [I
like the way C<LexPad>s and the C<errorson> settings provide the option
for exception-based or non-exception-based implementations, rather than
forcing one or the other.]

{{ NOTE: There are a couple of different factors here.  One is the
ability to globally define the severity of certain exceptions or
categories of exceptions without needing to define a handler for each
one. (e.g. Perl 6 may have pragmas to set how severe type-checking
errors are. A simple "incompatible type" error may be fatal under one
pragma, a resumable warning under another pragma, and completely silent
under a third pragma.) Another is the ability to "defang" opcodes so
they return error codes instead of throwing exceptions. We might provide
a very simple interface to catch an exception and capture its payload
without the full complexity of manually defining exception handlers
(though it would still be implemented as an exception handler
internally). Something like:

=begin PIR_FRAGMENT_TODO

  .local pmc error_code
  .capture_start error_code
  $P1 = find_lex 'foo'
  .capture_end

  # error_code contains what would have been the "error" return value

=end PIR_FRAGMENT_TODO

This could eliminate the need for "defanging" because it would be almost
as easy to use as error codes. It could be implemented once for all
exceptional opcodes, instead of needing to be defined for each one. And,
it still keeps the error information out-of-band, instead of mixing the
error in with normal return values. }}

=head3 Exception Object Interface

=head4 Retrieving the Exception Message

The exception message is stored in the 'message' attribute:

=begin PIR_FRAGMENT

  # ...
 handler:
  .local pmc exception
  .local string message
  .get_results (exception)
  message = exception['message']
  say message

=end PIR_FRAGMENT


=head4 Resuming after Exceptions

Exceptions thrown by standard Parrot opcodes (like the one thrown by
C<get_hll_global> above or by the C<throw> opcode) are always resumable,
so when the exception handler function returns normally it continues
execution at the opcode immediately after the one that threw the
exception. Other exceptions at the run-loop level are also generally
resumable.

{{NOTE: Currently only implemented for the actual throwing opcodes, throw,
die, exit.}}

You resume from an exception by invoking the return continuation stored
in the 'resume' attribute of the exception.

=begin PIR_FRAGMENT

  push_eh handler
  $P0 = new 'Exception'          # create new exception object
  throw $P0                      # throw it
  pop_eh
  say "Everything is just fine."
  .return()
 handler:
  .local pmc exception, continuation
  .get_results (exception)
  continuation = exception['resume']
  continuation()

=end PIR_FRAGMENT

=head2 References

F<src/ops/core.ops>, F<src/exceptions.c>, F<src/pmc/exception.pmc>,
F<src/pmc/exceptionhandler.pmc>

=cut

__END__
Local Variables:
  fill-column:78
End:
