[Top][All Lists]

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

Re: Checking parameters

From: Lennart Borgman (gmail)
Subject: Re: Checking parameters
Date: Sun, 17 Jun 2007 12:55:17 +0200
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20070604 Thunderbird/ Mnenhy/

Cecil Westerhof wrote:
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))

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
How should I generate an error/exception?

You probably want to use the most simple way, something like

(defun get-hours(time)
  (interactive "sHH:MM: ")
  (let ((timelist (split-string time ":"))
        (errmsg "Time format error"))
    (unless (= (length (nth 1 timelist)) 2)
      (error errmsg))
      (string-to-number (nth 0 timelist))
        (string-to-number (nth 1 timelist))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]