2013-08-31 22 views
5

在下面defun定義 ...Emacs lisp - 如何嘗試/ catch處理錯誤?

(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

拋出我怎麼會趕上錯誤並返回零或""

回答

3

re-search-backward-lax-whitespace有一個可選的noerror參數。

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

不會發出錯誤信號。

對於更一般的錯誤處理,您可以使用ignore-errorscondition-case。有關後者的信息,請參閱

Error Handling in Emacs Lisp

+0

好,謝謝@Barmar – ocodo