On 10/22/10 5:12 PM, scouic wrote:
Hi,
i learn with-handlers mechanism, and i don't understand what's wrong in
this piece of code ?

#lang racket
(require xml)

(define a-good-string "<doc><bold>hi</bold> there!</doc>")
(define a-bad-string "<doc><boooooold>hi</bold> there!</doc>")

(define (panoramix s)
   (with-handlers ((exn:invalid-xexpr? (lambda (e) 'UFO)))
     (string->xexpr s)))

(panoramix a-good-string)
; this is ok and returns > '(doc () (bold () "hi") " there!")

(panoramix a-bad-string)
; <unsaved editor>::6: read-xml: parse-error: start tag `boooooold' at
[1.5/6 1.16/17] doesn't match end tag `bold' at [1.18/19 1.25/26]

Why it returns an error and not UFO ?

Because the exception raised is not a value that answers true to exn:invalid-xexpr?. If you change the program to this, you can see what is raised:

(define (panoramix s)
  (with-handlers (((lambda (x) true) (lambda (e) e)))
    (string->xexpr s)))

which is:

(exn:xml
"read-xml: parse-error: start tag `boooooold' at [1.5/6 1.16/17] doesn't match end tag `bold' at [1.18/19 1.25/26]"
 #<continuation-mark-set>
 (list (srcloc 'string #f #f 6 11) (srcloc 'string #f #f 19 7))
 ...)

The problem is not in converting XML to an Xexpr, but rather in reading XML from a string. If you change the predicate to exn:xml? it will work as you expected.

(define (panoramix s)
  (with-handlers ((exn:xml? (lambda (e) 'UFO)))
    (string->xexpr s)))

David

_________________________________________________
 For list-related administrative tasks:
 http://lists.racket-lang.org/listinfo/users

Reply via email to