# How exceptions are delivered.
There are 3 separate parts
1. The exception deliver table, one per compiled method.
2. A translator that converts rescues into the exception table
3. The exception deliverer, which searches tables and perform jumps.
## The exception table.
This is the core of the exception deliver mechanism. The table
is organized into rows, each row having 3 columns:
1. the start bytecode index
2. the end bytecode index
3. the bytecode index to jump to
## How they all work together
When an exception is raised, the deliverer takes the current
context's exception table and the current ip and searches
the table to find out if there is an entry where the start
is less than ip and the end is greater than ip. Because
the table is pre-sorted to have the smallest entries
first, the first valid entry hit during the search will
always be the correct one. The deliver then sets the
bytecode interpreter's ip to the 3rd bytecode index in the entry.
This causes the interpreter to begin executing a rescue.
The first bytecodes of a rescue perform checking that the
current exception matches the criteria given in the 'rescue'
statement in ruby code (currently, just that the exception is
of a certain klass, done using ===).
At the end of these first bytecodes is a raise which
triggers the whole thing to occur again. This raise is jumped
past if the exception matches the criteria.
If the deliverer is unable to find a valid exception table entry,
it reactivates the current context's sender and starts over.
If the exception reaches the top context and is still unable to
find someone to handle the exception, the cpu is trigger to handle it
itself. This typically means informing the user in some way that an
exception has reached the top and that the program is going to terminate.
## Algorithms
find an exception handler:
I = @active_context.ip
T = @active_context.exceptions
while @active_context
T.each_row do |entry|
if entry[0] <= I and entry[1] >= I
@cpu.ip = entry[2]
return
end
end
@active_context = @active_context.sender
end