[plt-scheme] handle exception
Petros Pissias wrote:
> I'm trying to make a programm that draws a function.
> The only problem is that sometimes the function
> throws a 'divide by zero exception' that must be handled correctly.
>
> The user is prompted to give the function [example: (lambda (x) (/ 1
> x))]
>
> I don't want the user to give the exception handling details in the
> function
> [example: (lambda (x)
> (with-handlers ([exn:application:divide-by-zero?
> (lambda (exn) +inf.0)])
> (/ 1 x))) ]
>
> I'm trying to 'insert' the exception handling code after the "
> (define func (read))" which reads the function,
> but I can't get it right...
There is two options:
1. Redefine /
(define prim:/ /)
(define (/ a b)
(if (= b 0)
+inf.0
(prim:/ a b)))
2. Insert the with-handlers form where you evaluate the user specified
function
> (define (tabulate f from to)
(if (> from to)
'()
(cons (with-handlers ([exn:application:divide-by-zero? (lambda (e) +inf.0)])
(f from))
(tabulate f (+ from 1) to))))
> (tabulate / -2 2)
(-1/2 -1 +inf.0 1 1/2)
--
Jens Axel Søgaard