1 |
@c -*-texinfo-*- |
@c -*-texinfo-*- |
2 |
@c This is part of the GNU Emacs Lisp Reference Manual. |
@c This is part of the GNU Emacs Lisp Reference Manual. |
3 |
@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999 |
@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999 |
4 |
@c Free Software Foundation, Inc. |
@c Free Software Foundation, Inc. |
5 |
@c See the file elisp.texi for copying conditions. |
@c See the file elisp.texi for copying conditions. |
6 |
@setfilename ../info/control |
@setfilename ../info/control |
7 |
@node Control Structures, Variables, Evaluation, Top |
@node Control Structures, Variables, Evaluation, Top |
154 |
non-@code{nil}, @var{then-form} is evaluated and the result returned. |
non-@code{nil}, @var{then-form} is evaluated and the result returned. |
155 |
Otherwise, the @var{else-forms} are evaluated in textual order, and the |
Otherwise, the @var{else-forms} are evaluated in textual order, and the |
156 |
value of the last one is returned. (The @var{else} part of @code{if} is |
value of the last one is returned. (The @var{else} part of @code{if} is |
157 |
an example of an implicit @code{progn}. @xref{Sequencing}.) |
an example of an implicit @code{progn}. @xref{Sequencing}.) |
158 |
|
|
159 |
If @var{condition} has the value @code{nil}, and no @var{else-forms} are |
If @var{condition} has the value @code{nil}, and no @var{else-forms} are |
160 |
given, @code{if} returns @code{nil}. |
given, @code{if} returns @code{nil}. |
165 |
|
|
166 |
@example |
@example |
167 |
@group |
@group |
168 |
(if nil |
(if nil |
169 |
(print 'true) |
(print 'true) |
170 |
'very-false) |
'very-false) |
171 |
@result{} very-false |
@result{} very-false |
172 |
@end group |
@end group |
260 |
never @code{nil}, so this clause never fails, provided the @code{cond} |
never @code{nil}, so this clause never fails, provided the @code{cond} |
261 |
gets to it at all. |
gets to it at all. |
262 |
|
|
263 |
For example, |
For example, |
264 |
|
|
265 |
@example |
@example |
266 |
@group |
@group |
376 |
@var{conditions} turned out @code{nil}. (Think about it; which one |
@var{conditions} turned out @code{nil}. (Think about it; which one |
377 |
did not?) |
did not?) |
378 |
|
|
379 |
For example, this expression tests whether @code{x} is either |
For example, this expression tests whether @code{x} is either |
380 |
@code{nil} or the integer zero: |
@code{nil} or the integer zero: |
381 |
|
|
382 |
@example |
@example |
401 |
@example |
@example |
402 |
@group |
@group |
403 |
(if @var{arg1} @var{arg1} |
(if @var{arg1} @var{arg1} |
404 |
(if @var{arg2} @var{arg2} |
(if @var{arg2} @var{arg2} |
405 |
@var{arg3})) |
@var{arg3})) |
406 |
@end group |
@end group |
407 |
@end example |
@end example |
663 |
@end group |
@end group |
664 |
|
|
665 |
@group |
@group |
666 |
(catch 'hack |
(catch 'hack |
667 |
(print (catch2 'hack)) |
(print (catch2 'hack)) |
668 |
'no) |
'no) |
669 |
@print{} yes |
@print{} yes |
745 |
buffer. You can also signal errors explicitly with the functions |
buffer. You can also signal errors explicitly with the functions |
746 |
@code{error} and @code{signal}. |
@code{error} and @code{signal}. |
747 |
|
|
748 |
Quitting, which happens when the user types @kbd{C-g}, is not |
Quitting, which happens when the user types @kbd{C-g}, is not |
749 |
considered an error, but it is handled almost like an error. |
considered an error, but it is handled almost like an error. |
750 |
@xref{Quitting}. |
@xref{Quitting}. |
751 |
|
|
1000 |
@smallexample |
@smallexample |
1001 |
@group |
@group |
1002 |
(defun safe-divide (dividend divisor) |
(defun safe-divide (dividend divisor) |
1003 |
(condition-case err |
(condition-case err |
1004 |
;; @r{Protected form.} |
;; @r{Protected form.} |
1005 |
(/ dividend divisor) |
(/ dividend divisor) |
1006 |
@end group |
@end group |
1007 |
@group |
@group |
1008 |
;; @r{The handler.} |
;; @r{The handler.} |
1046 |
;; @r{This is a call to the function @code{error}.} |
;; @r{This is a call to the function @code{error}.} |
1047 |
(error "Rats! The variable %s was %s, not 35" 'baz baz)) |
(error "Rats! The variable %s was %s, not 35" 'baz baz)) |
1048 |
;; @r{This is the handler; it is not a form.} |
;; @r{This is the handler; it is not a form.} |
1049 |
(error (princ (format "The error was: %s" err)) |
(error (princ (format "The error was: %s" err)) |
1050 |
2)) |
2)) |
1051 |
@print{} The error was: (error "Rats! The variable baz was 34, not 35") |
@print{} The error was: (error "Rats! The variable baz was 34, not 35") |
1052 |
@result{} 2 |
@result{} 2 |
1096 |
@group |
@group |
1097 |
(put 'new-error |
(put 'new-error |
1098 |
'error-conditions |
'error-conditions |
1099 |
'(error my-own-errors new-error)) |
'(error my-own-errors new-error)) |
1100 |
@result{} (error my-own-errors new-error) |
@result{} (error my-own-errors new-error) |
1101 |
@end group |
@end group |
1102 |
@group |
@group |
1112 |
|
|
1113 |
The error string should start with a capital letter but it should |
The error string should start with a capital letter but it should |
1114 |
not end with a period. This is for consistency with the rest of Emacs. |
not end with a period. This is for consistency with the rest of Emacs. |
1115 |
|
|
1116 |
Naturally, Emacs will never signal @code{new-error} on its own; only |
Naturally, Emacs will never signal @code{new-error} on its own; only |
1117 |
an explicit call to @code{signal} (@pxref{Signaling Errors}) in your |
an explicit call to @code{signal} (@pxref{Signaling Errors}) in your |
1118 |
code can do this: |
code can do this: |