4

I'm new to emacs-lisp. I wrote a function that kills the buffer of my main work document work.org and opens specific project files.

(defun load-current-files ()
  (interactive)
  (kill-buffer "work.org") 
  (find-file "~/file-1.txt")
  (find-file "~/file-2.txt")
  (find-file "~/file-3.txt"))

The function works perfectly when work.org is open. However, if I've already killed that buffer, the function, very naturally throws an error saying Lisp error: (error "No buffer named work.org").

That makes perfect sense. But how do I tell a defun to just keep chugging along even if it encounters an error? Can I wrap a command in an (ignore-errors) layer or something?

(ignore-errors (kill-buffer "work.org"))

Something like that?

5

Yes, you can use the macro ignore-errors, it is a wrap of the special form condition-case, use the latter if you want to handle that error.


ELisp Ref: Writing Code to Handle Errors or (info "(elisp) Handling Errors").

  • 3
    condition-case is not only for handling errors. It also lets you selectively ignore only particular errors. ignore-errors, on the other hand, ignores all errors. See also with-demoted-errors. – Drew Oct 9 '15 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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