问题描述
一个简单的例子:
function! Foo()
echom 'Starting Foo'
let x = Bar(123) " Function does not exist so E117 occurs.
echom 'Nonetheless,this executes.'
endfunction
Vim 是否有机制在任何错误时中止函数或脚本
发生 - 换句话说,与 bash
中的此设置有相似的精神?
set -o errexit
澄清一下,我了解如何在 try-catch
中包装容易出错的代码。我是
寻找一种让 Vim 脚本表现得相似的通用机制
到 Python、Ruby、Perl 等语言,其中出现未处理的错误
导致执行停止。
解决方法
TL;博士
总是用 abort
标记函数
您的问题最近在 vi.SE 上有重复:https://vi.stackexchange.com/questions/29038/how-do-i-make-a-function-stop-when-an-error-occurs/29039#29039 或者,关于同一主题:https://vi.stackexchange.com/questions/15905/when-should-a-function-not-be-defined-with-abort
在 SO 上,还有一些关于 abort
的解释:Vimscript: what's the point of 'abort' in a function definition?