How do you return early from a function before it's ended? For example:
(defun my-func () "for example." (unless something (return nil)) ; continue as usual... (+ 42 1))
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.Sign up to join this community
In addition to what @EmacsFodder covered, just raise an error.
This will not help if the code is called within (dynamically, not lexically) the extent of error-handling constructs such as
condition-case, but otherwise it is a fine way to exit a function. It is in fact what is done most of the time.
(defun my-func () "..." (unless something (error "Whoops!")) ; continue as usual... (+ 42 1))
If you want to handle the error yourself then you can put the calling code (e.g. the call to something that ulimately calls
my-func) inside a
Again, this is what is done most of the time, at least as often as using
throw. It all depends on what behavior you want.
We have a number of options available.
throw to exit the function.
(defun my-func () "thrown error" (catch 'my-catch (when t (throw 'my-catch "always going to throw")) (+ 42 1)))
You can also use
return-from (although you will need to require
(require 'cl-macs) (defun my-func () "block / return-from" (block my-func (when t (return-from my-func)) (+ 42 1)))
We also have
cl-defun which has an implicit
block with the same name as the function, so we can do the
block style with less.
(require 'cl-macs) (cl-defun my-func () "cl-defun implicit block" (when t (return-from my-func)) ; my-func is an implicit block. (+ 42 1)))
cl-defun is also available as an alias
defun* which is defined in
(require 'cl) (defun* my-func () "defun* implicit block" (when t (return-from my-func)) ; my-func is an implicit block. (+ 42 1)))