help-gnu-emacs
[Top][All Lists]
Advanced

[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:1.8.1.4) Gecko/20070604 Thunderbird/2.0.0.4 Mnenhy/0.7.5.666

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


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





reply via email to

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