From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tim X Newsgroups: gmane.emacs.help Subject: Re: Checking parameters Date: Sun, 17 Jun 2007 23:18:54 +1000 Organization: Posted via Supernews, http://www.supernews.com Message-ID: <87sl8qsoqp.fsf@lion.rapttech.com.au> References: <46750a24$0$338$e4fe514c@news.xs4all.nl> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1182087635 531 80.91.229.12 (17 Jun 2007 13:40:35 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sun, 17 Jun 2007 13:40:35 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Jun 17 15:40:33 2007 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1HzuzZ-0000JY-D1 for geh-help-gnu-emacs@m.gmane.org; Sun, 17 Jun 2007 15:40:33 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1HzuzY-0002wq-Pu for geh-help-gnu-emacs@m.gmane.org; Sun, 17 Jun 2007 09:40:32 -0400 Original-Path: shelby.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!sn-xt-sjc-03!sn-xt-sjc-09!sn-post-sjc-02!sn-post-sjc-01!supernews.com!corp.supernews.com!not-for-mail Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1.50 (gnu/linux) Cancel-Lock: sha1:DcJSyk/yxPZdOI+b5mT5KAp4+b0= Original-X-Complaints-To: abuse@supernews.com Original-Lines: 67 Original-Xref: shelby.stanford.edu gnu.emacs.help:149542 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:45131 Archived-At: Cecil Westerhof writes: > I am new to Emacs and lisp. > What is the best way to check parameters and do error handling when they are > not correct? > > I have the following function: > (defun getHours(time) > (interactive "sHH:MM: ") > (let ((timelist (split-string time ":"))) > (+ > (string-to-number (car timelist)) > (/ > (string-to-number (cadr timelist)) > 60.0 > ) > ) > ) > ) > > How do I check that there is exactly one parameter? And how do I check the > format and give an error that works in interactive and normal mode? > For example when I give > (getHours "0:120") > I get > 2.0 > How should I generate an error/exception? > Emacs will throw an error if the function is called with the wrong number of arguments. There is already a reply that shows a simple way to check for the correct string format using a regexp. However, if you want to get a bit more adventurous and you would like a function that can tell the user the arguement is invalid and give them another opportunity to enter a correctly formatted argument, you might want to use a function instead of the prompt sring. This function can then prompt the user for input, check that it is valid and if it is, just returns the values as a list (which may only have one element if thats all that is required). If the value the user entered was not a valid format, the function can print an error message, possibly explaining why the users input was no good and then prompt again to give the user another go. While this may take more work, it can be nicer for the end user. It can sometimes also make your code look a bit cleaner as you can put the arguement error checking off in its own function, rather than have it cluttering up the main body of your function. for example (untested) (defun time-prompt () "Prompt for a valid time string HH:MM." (let (prompt-str) (setq prompt-str (read-from-minibuffer "Time HH:MM: ")) (while (not (string-match "^[0-9]+:[0-5][0-9]$" prompt-str)) (message "Bad time format. Must be HH:MM") (setq prompt-str (read-from-minibuffer "Time HH:MM: "))) (list prompt-str))) (defun getHours(time) (interactive time-prompt) .... tim -- tcross (at) rapttech dot com dot au