2013-08-31 32 views
5

Trong defun sau ...Emacs lisp - làm thế nào để thử/bắt xử lý một lỗi?

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

Nếu không phù hợp được tìm thấy, một lỗi được ném bởi re-search-backward-lax-whitespace

Làm thế nào tôi có thể bắt lỗi và trở về con số không hoặc ""?

Trả lời

3

re-search-backward-lax-whitespace có đối số noerror tùy chọn.

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

sẽ không báo hiệu lỗi.

Để xử lý lỗi tổng quát hơn, bạn có thể sử dụng ignore-errors hoặc condition-case. Để biết thông tin về trường hợp sau, hãy xem

Error Handling in Emacs Lisp

+0

Tuyệt vời, cảm ơn @Barmar – ocodo

Các vấn đề liên quan