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.
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
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
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)
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
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