gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:45:26 26 4
gpt4 key购买 nike

在以下 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 或 "" ?

最佳答案

re-search-backward-lax-whitespace有一个可选的 noerror争论。

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

不会发出错误信号。

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

Error Handling in Emacs Lisp

关于Emacs lisp - 如何尝试/捕获处理错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18543673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com