method HELLO: Everybody's favorite program. PRINT "Hello world!" return
Note that the language is not case sensitive. The examples are formatted using a pretty printer.
> gilda -program hello :Compile the program. gilda: Building hello gilda: hello.g --> obj/hello.o > hello :Run the program. Hello world!
Gilda has a novel exception handling mechanism that programmers can use to write more reliable programs. In the Hello program an exception could occur if the standard output stream is broken. The method would raise an exception and a last chance exception handler in the run time system would attempt to print a message containing a context string and a descriptive message to the standard error stream.
Flush) Could not write to a stream.
Since the standard error and output streams are often the same the program would likely raise another exception. The last chance handler in the run time system does not contain an exception handler and in this event it quietly terminates. If instead you want to handle the exception, the simplest way is to add a coarse grain exception handler. It attempts to beep when it catches an exception. Note that the handler method is coded in the same file as the primary method.
method HELLO: The example program with an exception handler. PRINT "Hello world!"; Printing can raise an exception. return method catch: Catch any exceptions raised in the method body. WARNING_BEEP; Standard library method to make a jazz noise. return
The library method, WARNING_BEEP, needs to be imported; which is done inside a class. Class files declare the usual items like user types, static variables and member methods, but also declare build dependencies so that Gilda programs know how to build themselves.
class Hello: A class file for the Standard hello example program. import Platform :Access standard platform specific utilities. gilda Hello ROOT :Declare the Hello method as the main entry point. return :A class Return simply designates the end of the file.
Nontrivial programs contain classes and are compiled by designating the top level class in the class hierarchy. To rebuild an entire program the Clean switch is used. Otherwise the compiler performs an incremental build. Keywords can be abbreviated to single letters for convenience. The last line in the verbose compiler output uses an intermediate generated assembler file named cm_run_catch.s which is translated into an object module containing exception information.
> gilda -c -p hello :Compile the program. gilda: Building hello gilda: hello.g --> obj/hello.o asm: asm/cm_run_catch.s --> obj/cm_run_catch.o
Now when the program runs it will either print, beep, do nothing (if the beeper is disabled), or it could raise another exception. The latter case occurs when the program is run in a terminal emulator and the WARNING_BEEP method is implemented by writing a control-G to the console via the standard error stream. In this situation the exception raised in WARNING_BEEP is nested in an exception handler.
The run time system detects nested exceptions to avoid run away circular exceptions. If the programmer wants to permit nested exceptions the Retry attribute can be added to the "method catch" line. This is useful when writing handlers that want to retry an operation several times before giving up. When no Retry attribute is coded then a Retry fault exception is raised.
Fault exceptions are raised when a program has run amok and is usually indicative of a bug. They can also be raised by an Assertion statement; which is useful when debugging and supports the Contract Programming methodology [Meyer]. Methods can exit normally at a Return statement or they can exit by raising an exception or a fault.
Faults are treated distinctly from normal exceptions in Gilda programs. After a fault a typical strategy is to contain damage and retreat to a higher level or gracefully terminate. The context of a fault is less reliable due to potential corruption and correspondingly language support for faults is simpler and more coarse. The next example shows how we could catch the fault and quietly terminate.
method HELLO: The example program with exception and fault handlers. PRINT "Hello world!"; Printing can raise an exception. return method catch fault: Catch any faults that arise. return; Quietly exit. method catch: Catch any exceptions raised in the method body. WARNING_BEEP; Standard library method to make a jazz noise. return
We could still run into a problem with this program when it is run and there is no memory left. In this scenareo after the program launches it attempts to enter the Hello method, but when it tries to allocate a stack frame it runs out of memory. The run time system detects this and raises a Stack Overflow fault to the last chance handler. It would either print a diagnostic message or failing that quietly terminate. If you wanted to explicitly manage the fault you could write a handler to override the default last chance handler.
This example illustrates general concepts for exception handling in Gilda. Exceptions are managed separately from faults. Handlers can be incrementally added without perturbing code in the body of the primary method. Contrast this with try-throw-catch; which would implement similar behavior by wrapping the Print statement in a Try clause and embed a handler within it as well [Malayeri]. Gilda lets you write well behaved programs that thoroughly check exceptions without becoming cluttered. You can incrementally add new handlers to existing programs; which is a natural way for programmers work.
Also notice that exceptions are not named. Named exceptions are not necessary for precise exception handling and when used need to be carefully managed. Anonymous exceptions are not only less hassle to code, but also avoids problems with name conflicts and inappropriately overloaded names [Miller].
What this example does not illustrate is how exceptions are managed with more precision. It only hints at how context is propagated and does not show how exceptions are specified. If you are curious then the easiest way to learn more is to get the Gilda compiler and start coding.
1. Bertrand Meyer. "Applying Design by Contract". IEEE Computer, pages 40-51, October 1992.
2. Donna Malayeri and Jonathan Aldrich. "Practical Exception Specifications", volume 4119/2006, pages 200-220. Springer Berlin / Heidelberg, October 2006. ISBN 978-3-540-37443-5.
3. Robert Miller and Anand Tripathi. "Issues with Exception Handling in Object-Oriented Systems". In European Conference on Object-Oriented Programming (ECOOP), volume 1241, pages 85-103, September 1997.