Cind programming language

Language definition Examples Download Contact

Exception handling

Exception handling is a mechanism that provides termination of code execution, usually in case of some error occuration, by throwing an exception and catching it in the other place, from where that code execution was called. An exception thrown from some place of code, stops further code execution, and goes back in the calling stack of current thread, up to the place, where thrown exception may be catched.
An exception is thrown by instruction throw and catched by try statement.

Throwing exception

An instruction to throw an exception is:

throw e;

where e is an exception object.
This instruction doesn't return any value, it terminates current code execution and throws given exception.

Example:

if (x < 0) { throw new exception("x < 0"); } else if (x >= 10) { throw new exception("x >= 10"); } // ok, here 0 <= x < 10 ...

Try statement

Try statement allows to catch exception thrown from execution of given code.
General syntax is:

try { // ... code from which will catch an exception } catch(type1 e) { // ... code executed when exception type match type1 } catch(type2 e) { // ... code executed when exception type match type2 } catch(e) { // ... }

When exception is thrown from the main code of try statement, then its type is compared with types declared in the catch sections, in specified order. After first match, the code of selected catch section are being executed, with exception putted in declared variable, which will be visible only inside that catch section. When there is no type declared (for instance: in the third section in above code), then this section will be chosen unconditionally. When exception doesn't match any catch section, it is thrown out again, as if it has not been catched. When main code of try statement doesn't throw any exceptions, then none of the followed catch sections are going to be executed.

Type comparison is done using the operator >>= (see Operations on types).
Notice also, then type written in catch section header is not the type of variable, but is used for comparison, and so, the catched exception can be an object of different type.

Exception object

Exception object is a regular object, which has implemented followed methods:

method parameters description
isException () returns true
clone () returns new exception object with the same information
addTrace (source) adds source information to the trace list
describe () returns string description of exception in visual form
with max 16 lines of trace

This methods are used by execution software during exception handling process.

It is possible to implement own exception class, to do that, it must have implemented all above methods (see Classes).
Typical exception class implementation just inherits from cind.exception class, and therefore, it only needs to implement own clone() method.

Example of own exception class:

class myexception extends cind.exception { // ... own data stored in exception init() { // ... under("MyException"); } .clone() { // create new exception object var ex = new myexception(); // ... set own information in new object here // set trace information from this object to new exception ex.setTraceFrom(this); // done return ex; } // ... }

Finally, to throw an exception of own class, declared above, write:

throw new myexception();

Exception trace

A trace is a list of places through which exception was thrown, written in visual form.
An exception trace list includes any context places, methods, functions, sections, and so on, starting from the place, where exception was thrown, and ending where exception was catched.

To get trace of exception in visual form use describe method of exception object.
Example of retrieved trace:

exception: index out of bounds at: mypack.second:process at: mypack.second:<init> at: mypack.myclass:fun() at: mypack.myclass.f at: <execute>


Lines in the trace are added by execution software when exception handling is being performed.
Getting the trace is basically useful to find a place where exception was thrown,
usually to find code snippet that works incorrectly.



Cind programming language 1.0.4