3

There are several wordings for this depending on what programming languages you are used to, but none of them turned up results for me. Also, neither help abort() nor help raise() nor help throw() gave me anything.

How would you explicitly throw an error? How would you explicitly raise an error? How would you explicitly abort?

I have in the past just written my errormessage directly, which is of course invalid code and raises an error, but I'd prefer to be clear about what I'm doing here.

function may_fail() abort
  let l:message = system('something')
  if v:shell_error
    there was a problem with something <- can I use an actual abort() or something here?
  endif
endfunction
4
  • 1
    :h throw-catch?
    – muru
    Commented Mar 13, 2021 at 6:41
  • Yes indeed, thanks! I see now that :h throw was what I needed, but normally for vimscript questions I'm supposed to add () to find functions. Commented Mar 13, 2021 at 6:43
  • 2
    throw is an Ex command not a function. :) The magic string for those is the : prefix rather than the () suffix: :h :throw.
    – B Layer
    Commented Mar 13, 2021 at 6:46
  • 3
    If you're not sure which one it is two options are 1) do a general search with :helpgrep or 2) look for it in both :h function-list and :h ex-cmd-index.
    – B Layer
    Commented Mar 13, 2021 at 6:55

1 Answer 1

3

Ok, I found the answer a little later on StackOverflow. It's not clear to me whether these vimscript questions should be on vi.stackexchange or on SO.

Use echoerr to output an error and continue, and throw to abort / raise an error:

echoerr 'some command failed with: '
throw l:output
3
  • 3
    Vimscript is absolutely on-topic here. Anything related to official VIm and NeoVim is.
    – B Layer
    Commented Mar 13, 2021 at 6:51
  • 2
    Technically, anything vi-like enough :)
    – D. Ben Knoble
    Commented Mar 13, 2021 at 13:57
  • Well, my answer wasn't intended to be comprehensive but, yes. :)
    – B Layer
    Commented Mar 15, 2021 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.