Links

Categories

Tags


« | Main | »

Exceptions

By Jewe | October 29, 2012

class WrongTypeException : exception
{
    string message;

    method WrongTypeException(const string msg) { message = msg; }
    method int getError() { return typeof(this); }
    method string getMessage() { return message; }
}

function string main(const string[] args)
{
    int[] values   = { 10, 20, 30 };
    string[] names = { "Judy", "Christopher", "Helen", "James", "Sandra", "Rick", "Elisabeth" };

    // increment all array elements by 1
    values = values.process( Incrementor, null );
    names  = names.process ( Incrementor, null ); // this will throw in Incrementor()

    return "We will never get here";
}

function var Incrementor(var element, var args)
{
    if( typeof(element) != typeof(int) )
        throw new WrongTypeException("element is not int in Incrementor(var element, var args)");

    return element + 1;
}

Topics: code examples | Comments Off on Exceptions

Comments are closed.