Contents | Prev | Next | Index


Exception Handling

An exception is generally an error condition or other event that interrupts the normal flow of execution in a program. When an exception is raised, it causes control to be transfered from the current point of execution to an exception handler. Canterbury Pascal has an exception handling support which provides a structured of separating normal program logic from error handling logic, greatly increasing the maintainability and robustness of applications.

Canterbury Pascal uses objects to represent exceptions. This has several adavantages, such as:

Canterbury Pascal uses the following Java exception classes which can be extended for introducing new exception kinds:

Exceptions and errors are instances of above mentioned (possxibly extended) classes. Instances of classes extended from java.lang.Error or java.lang.RuntimeException may be thrown at any time during program execution, usually resulting in a program abortion. They can, but must not, be caught and be dealt with by the program. Instances of java.lang.Exception and its extensions (except for java.lang.RuntimeException) are always to be caught and to be dealt with in the program. These are known in Java as checked exceptions.

If a checked exception is thrown in a procedure, function or method body, e.g. as a result of calling a foreign Java method, this Pascal compiler generates a default exception handler by wrapping the statement block in a Java try-catch-statement, as follows:

try
{
  statements
}
catch (java.lang.RuntimeException _e)
{ throw _e; }
catch (java.lang.Exception _e)
{ SYSTEM.CANCEL( _e ); }

That is, any exceptions other than those derived from java.lang.RuntimeException lead to a program abortion. An exception derived from java.lang.RuntimeException is being re-thrown in the default handler, hence it can be dealt with by any of the current method's callers. If no method deals with this kind of exception, the Java default action is to abort the program.

Notice that Borland Object Pascal, unlike Canterbury Pascal, uses a different class hierarchy for exception handling, based on SYSTEM.Exception, which is an extension of SYSTEM.TObject. Canterbury Pascal by contrast uses Java's exception classes.

The following statements are available in Canterbury Pascal for raising and handling exceptions:

Try-Except Statements

Try-Finally Statements

Raise Statements


Contents | Prev | Next | Index

Canterbury Pascal for JVM  (Last documentation update Sep 02, 2004)
Copyright © 1999-2004 J.Neuhoff - mhccorp.com  . All rights reserved.
Please send any comments or corrections to neuhoff@mhccorp.com