From: smartnose
Subject: How to handle errors while reading?
Date: 
Message-ID: <803fe952-7c2a-4336-b918-d5ed0b5f4565@v53g2000hsa.googlegroups.com>
I want to read user's input using read, but what if the user's input
is in a wrong format?

Can I  check this error like the exception handling in Java? So I can
let him/her to input again.

try{
..
}
catch
...

Thanks.

W.

From: Tamas K Papp
Subject: Re: How to handle errors while reading?
Date: 
Message-ID: <6mi02kFgpg4oU1@mid.individual.net>
On Sat, 25 Oct 2008 18:21:34 -0700, smartnose wrote:

> I want to read user's input using read, but what if the user's input is
> in a wrong format?
> 
> Can I  check this error like the exception handling in Java? So I can
> let him/her to input again.
> 
> try{
> ..
> }
> catch
> ...

Of course.  A good introduction to CL's condition system is 

http://gigamonkeys.com/book/beyond-exception-handling-conditions-and-restarts.html

Read the whole book while you are at it, it is very good.

HTH,

Tamas
From: William James
Subject: Re: How to handle errors while reading?
Date: 
Message-ID: <ge0l3k0ulu@enews5.newsguy.com>
smartnose wrote:

> I want to read user's input using read, but what if the user's input
> is in a wrong format?
> 
> Can I  check this error like the exception handling in Java? So I can
> let him/her to input again.
> 
> try{
> ..
> }
> catch
> ...
> 
> Thanks.
> 
> W.

Exception handling isn't needed for this.

Ruby:

begin
  print "Enter username: "
  input = gets.strip
  break if input.match( /^\w+$/ )
  puts "\aInvalid username."
end until false
From: William James
Subject: Re: How to handle errors while reading?
Date: 
Message-ID: <ge0lun0v8a@enews5.newsguy.com>
William James wrote:

> smartnose wrote:
> 
> > I want to read user's input using read, but what if the user's input
> > is in a wrong format?
> > 
> > Can I  check this error like the exception handling in Java? So I
> > can let him/her to input again.
> > 
> > try{
> > ..
> > }
> > catch
> > ...
> > 
> > Thanks.
> > 
> > W.
> 
> Exception handling isn't needed for this.
> 
> Ruby:
> 
> begin
>   print "Enter username: "
>   input = gets.strip
>   break if input.match( /^\w+$/ )
>   puts "\aInvalid username."
> end until false

Let's make it look like Lisp.

(((print "Enter username: ")
  (input = gets.strip)
  (break if input.match( /^\w+$/ ))
  (puts "\aInvalid username.")
 ) until false)
From: Pascal Bourguignon
Subject: Re: How to handle errors while reading?
Date: 
Message-ID: <49043cad$0$24019$426a74cc@news.free.fr>
William James wrote:
> Let's make it look like Lisp.
> 
> (((print "Enter username: ")
>   (input = gets.strip)
>   (break if input.match( /^\w+$/ ))
>   (puts "\aInvalid username.")
>  ) until false)
> 

That's better.

Now try:

(defmacro cond (clauses)
   (if clauses
      (let ((vtest (gensym)))	
         `(let ((,vtest ,(caar clauses)))
             (if ,vtest
               (if (cdar clauses))
                 `(progn ,@(cdar clauses))
                  vtest)
               (cond ,@(cdr clauses)))))
	
in MatzLisp...

-- 
__Pascal Bourguignon__
http://www.informatimago.com
From: Pascal Bourguignon
Subject: Re: How to handle errors while reading?
Date: 
Message-ID: <49043a03$0$19730$426a74cc@news.free.fr>
William James wrote:
> smartnose wrote:
> 
>> I want to read user's input using read, but what if the user's input
>> is in a wrong format?
>>
>> Can I  check this error like the exception handling in Java? So I can
>> let him/her to input again.
>>
>> try{
>> ..
>> }
>> catch
>> ...
>>
>> Thanks.
>>
>> W.
> 
> Exception handling isn't needed for this.
> 
> Ruby:
> 
> begin
>   print "Enter username: "
>   input = gets.strip
>   break if input.match( /^\w+$/ )
>   puts "\aInvalid username."
> end until false

Wrong!


Exception handling isn't needed for this.

Use READ-LINE instead of READ, and then parse the input string as you 
want to check the format (you can use regular expressions if that's 
called for).



-- 
__Pascal Bourguignon__
http://www.informatimago.com
From: Alex Mizrahi
Subject: Re: How to handle errors while reading?
Date: 
Message-ID: <49042d60$0$90273$14726298@news.sunsite.dk>
 WJ> Ruby:

what is your point? 
From: Alex Mizrahi
Subject: Re: How to handle errors while reading?
Date: 
Message-ID: <4903c978$0$90272$14726298@news.sunsite.dk>
 s> I want to read user's input using read, but what if the user's input
 s> is in a wrong format?

what are you trying to read? s-expressions or something else?

 s> Can I  check this error like the exception handling in Java? So I can
 s> let him/her to input again.

 s> try{
 s> ..
 s> }
 s> catch
 s> ...

see handler-case