We will present the Green exception system using an example. In Java, we would use
try {
makeJuice();
}
catch ( BananaException
exc ) {
...
}
catch ( FruitException
exc ) {
...
}
to catch exceptions thrown by makeJuice
which should have been declared as
void makeJuice()
throws FruitException, BananaException;
In Green, this code is written as
catch = CatchFruitBananaException.new();
try(catch)
makeJuice();
end
in which makeJuice is declared as
proc makeJuice()
( exception : CatchFruitBananaException )
and CatchFruitBananaException declared as
class CatchFruitBananaException
subclassOf CatchUncheckedException
public:
proc
throw( x : FruitException )
begin
...
end
proc
throw( x : BananaException )
begin
...
end
end
The exception treatment should be put
in the throw methods. If an exception
is thrown inside makeJuice, a message
throw
is sent to object catch that treats
the exception. An exception is thrown by sending a message to exception:
exception.throw( FruitException.new() );
The previous example might have been written as follows if the method bodies of CatchFruitBananaException were empty. That is, the exception treatment is put in the case statement.
try( catch = CatchFruitBananaException.new()
)
makeJuice();
end
case catch.getClassException()
of
FruitException
:
begin
...
end
BananaException
:
begin
...
end
end { case }
The hierarchy of predefined Green exceptions is in the
table that follows.
Exception
TypeErrorException WrongParametersException NotFoundException PackedException TooManyDimensionsException MetaException ClassNotInAllowedSetException NoShellException NoExtensionException UncheckedException StackOverflowException IllegalArrayIndexException OutOfMemoryException InternalErrorException MessageSendToNilException NoReflectiveInfoException NoReflectiveBodyInfoException NoReflectiveCallInfoException ArithmeticException DivisionByZeroException RealOverflowException RealUnderflowException AssertionException
|