What's the best way in elisp to trap an error case

Viewed 1858

I'm trying to augment the etags-select functions so it will fall-back to a normal find-tag if find-tag at point failed. The code I've tried is:

(defun my-etags-find-tag ()
  "Find at point or fall back"
  (interactive)
  (unless (etags-select-find-tag-at-point)
    (etags-select-find-tag)))

(global-set-key (kbd "C-f") 'my-etags-find-tag)

However this fails when point is not at a valid tag. Instead I get a error thrown by etags-select-find-tag-at-point:

etags-select-find-tag-at-point: Wrong type argument: char-or-string-p, nil

In this case I just have to repeat the test done by etags-select-find-tag-at-point:

(defun my-etags-find-tag ()
  "Find at point or fall back"
  (interactive)
  (if (find-tag-default)
      (etags-select-find-tag-at-point)
    (etags-select-find-tag)))

But it does seem a little redundant. Is it possible to trap exceptions and do alternate processing in elisp?

2 Answers
Related