Emacs lisp - 如何尝试/ catch处理错误?

时间:2013-08-31 01:44:18

标签: emacs error-handling elisp

在以下 defun ...

(defun re-backward-match-group (rexp &optional n)
  "Grab the previous matches of regexp and return the contents of
  the n match group (first group match if no n arg is specified)"
  (save-excursion
  (unless n
    (setq n 1))
  (when (numberp n)
    (when (re-search-backward-lax-whitespace rexp)
      (when  (= (+ 2 (* n 2)) (length (match-data)))
        (match-string-no-properties n))))))

如果未找到匹配项,则re-search-backward-lax-whitespace

会引发错误

我如何捕获该错误并返回nil或""

1 个答案:

答案 0 :(得分:3)

re-search-backward-lax-whitespace有一个可选的noerror参数。

(re-search-backward-lax-whitespace rexp nil t)

不会发出错误信号。

对于更常见的错误处理,您可以使用ignore-errorscondition-case。有关后者的信息,请参阅

Error Handling in Emacs Lisp