Steve Wampler wrote:
...
For a while, I thought about trying to implement an exception handling
mechanism using co-expressions, but just never had the heart to dive in.

This got me thinking about it again, so I've implemented a simple Exception handling mechanism for Unicon. (It's missing the ability to produce a stack trace because I don't have my Unicon book at home and couldn't figure out how to do so without it! Suggested fixes are *welcome*.) I think this is a pretty interesting use of co-expressions.

I haven't tested it much - in fact, the simple program that follows is
the *only* test I've run!  There are two basic classes:

   Exception:  the object that represents the exception

   Try: the mechanism that implements the exception handling (really
       a catch/throw mechanism).  A robust version of this could be
       embedded directly in Unicon and given a cleaner syntax :)

Both of these are found in the Exceptions package at:

   http://tapestry.tucson.az.us/unicon

(they are built on other classes in the above library, but could be
implemented on top of Rob Parlatt's work just as easily...)

Here's a simple example of the use (this is the ExceptionTest
sample program also found at the above site):


import Exceptions

   #<p>
   #  A simple example of subclassing the base Exception class.
   #</p>
   class NumException : Exception()
   end

    #<p>
    #  Test of Exception handling.
    #  A numeric argument <> three runs without Exception.
    #  A non-numeric argument throws NumException while
    #  a call with the argument 3 throws an Exception (base class for
    #  all Exceptions).
    #</p>
    procedure main(args)

        i := get(args) | 0

        case x := Try().call{ f(i) } of {  # Note that call is a PDCO!

            Try().isException(x, "NumException"):
                    write("NumException: ",x.getMessage(),": ",
                                           x.getLocation())

            Try().isException(x):
                    write(x.getMessage(),": ",x.getLocation())

            default: write("x is '",x,"'.")
            }

    end

    #<p>
    #  Simple function that performs (meaningless) work but can
    #    throw two different types of Exceptions.
    #</p>
    procedure f(i)

        if numeric(i) = 3 then {
           Exception().throw("bad value of "||i)
           }
        else if not numeric(i) then {
           NumException().throw("'"||i||"'")
           }

        return i
    end

I hope someone finds this interesting.

Steve
--
Steve Wampler     [EMAIL PROTECTED]
The gods that smiled upon your birth are laughing now. -- fortune cookie


------------------------------------------------------- This SF.Net email is sponsored by: NEC IT Guy Games. Get your fingers limbered up and give it your best shot. 4 great events, 4 opportunities to win big! Highest score wins.NEC IT Guy Games. Play to win an NEC 61 plasma display. Visit http://www.necitguy.com/?r=20 _______________________________________________ Unicon-group mailing list Unicon-group@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/unicon-group

Reply via email to