Carapace

Error Handling: an example

This example of error handling uses two functions:

handler the function which handles errors
thrower the function which potentially throws errors

The script fragment is as follows:

    # a function which may throw an error
    #
    (defun Integer thrower ( (n Integer) )

        (if (< n 0)

            # yes indeedy...throw that error!
            (throw (create Error 100 1 "I don't like negative numbers"))

            # otherwise, check for large numbers
			(if (> n 1000)

				(throw (create Error 101 2 "I don't like large numbers"))

				# otherwise, all is well
				(printAux "Thankyou.... I like the number " n "\n")
			)
        )

        (return n)
    )

    # the function which handles errors
    #
    (defun NIL handler ( (n Integer) )

        (local  (err Error)     # required since we catch errors here
        )

        (try

            (do

                (printAux "the number is " n "\n")

                # call the function which might throw an error
                (thrower n)

                (printAux "...well...we survived!\n")
            )

            # we know that the function 'dangerous' may throw errors
            # with errorClass = 100
            #
            (catch 100

                (printAux "CAUGHT: the following error: " err.string)
            )
        )

        (return ())
    )

Notes:


Contents Index Current topic: objects Related topics: functions