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

Re: question about error signaling



On Wed Aug  3, Andrew Begel asks how to trap the MCL error
that occurs when (read-from-string "(a") is executed.
> I tried to trap MCL's end-of-file condition I'm unable 
> to catch it.  (handler-case (exp)
                        (end-of-file () blah))
> Does MCL output signals in that way? Or is there some way I can catch it?

MCL supports the standard handler-case macro that allows
the programmer to process errors. Unfortunately, an end-of-file error
is not generated when the string "(a " is read. Instead a
simple-error condition is generated.

Here's an example that uses handler case to
handle the type-error and simple-error in the 
read-from-string function.

You can also add an (error (condition) ... )
to handle other errors.  The attached code is a start.

mark

(loop for string in '("a b c" " " 2 "2x" "fred" "\"unterminated string")
      with value and position
      do (handler-case 
           (progn
             (multiple-value-setq (value position) 
               (read-from-string string nil '*eof*))
             (format t "~&position=~d, value=~a~%" position value))
           (type-error (condition) 
                       (with-slots (ccL::datum
                                    ccl::expected-type
                                    ccl::format-string) condition
                         (format t ccl::format-string ccl::datum 
ccl::expected-type) nil))
           (simple-error (condition) 
                         (with-slots (ccl::format-string ccl::format-arguments) 
condition  
                           (format t ccl::format-string ccl::format-arguments) 
nil))))

Here's the output created when running the program. 

position=2, value=A
Unexpected end of file encountered.value 2 is not of the expected type 
(SATISFIES STRINGP).
position=2, value=2X
position=4, value=FRED
Unexpected end of file encountered.
NIL