[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

exceptions



In python, I might write:
(outlook might stuff up the indentation...)

C:\>python
Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def divider(x):
...     return lambda y : y / x
...
>>> halver = divider(2)
>>> halver(9)
4 #integer division is the default, but that's changing...
>>> halver(4)
2
>>> bomber = divider(0)
>>> bomber(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in <lambda>
ZeroDivisionError: integer division or modulo by zero

and I can catch exceptions in a very straightforward way:

>>> try:
...     bomber(4)
... except:
...     print "you moron -- something went wrong"
...
you moron -- something went wrong

and I can choose which class of exceptions I want to catch

>>> try:
...     bomber(4)
... except ArithmeticError:
...     print "something went wrong with the arithmetic"
...
something went wrong with the arithmetic

and even which subclass of ArithmeticError I want to catch

>>> try:
...     bomber(4)
... except ZeroDivisionError:
...     print "You tried to divide by zero!"
...
You tried to divide by zero!
>>>

I am sure that all this can be done in mzscheme, but I wonder if there is a
simple "transliteration" of the above examples
to get started. The documentation seems to quite quickly get into custodians
and theads and parameters. I wonder if that is necessary in a dumb
single-threaded example like the above?

Cheers and thanks

Chris Wright