Mercurial > hg > octave-nkf
annotate test/try.tst @ 17249:923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
* oct-parse.in.yy (except_command): Handle exception identifiers.
* oct-parse.in.yy, parse.h (octave_base_parser::make_try_command):
New arg, ID.
* pt-except.cc, pt-except.h (tree_try_catch_command::expr_id): New
data member.
(tree_command::tree_command): Initialize it.
(tree_command::~tree_command): Delete it.
(tree_command::identifier): New function.
* pt-check.cc (tree_checker::visit_try_catch_command): Check for valid
expr_id.
* pt-pr-code.cc (tree_print_code::visit_try_catch_command):
Print expr_id.
* pt-eval.cc (tree_evaluator::visit_try_catch_command): Assign message
and identifier to variable.
* try.tst: New test.
* NEWS: Note change.
author | Stefan Mahr <dac922@gmx.de> |
---|---|
date | Tue, 13 Aug 2013 19:35:53 +0200 |
parents | 333243133364 |
children | e6c0ac8ce5b6 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14131
diff
changeset
|
1 ## Copyright (C) 2006-2012 John W. Eaton |
7016 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
5590 | 19 %!test |
20 %! try | |
21 %! catch | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
22 %! error ("Shoudn't get here"); |
5590 | 23 %! end_try_catch |
24 | |
25 %!test | |
26 %! try | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
27 %! clear a; |
5590 | 28 %! a; |
29 %! catch | |
30 %! end_try_catch | |
31 %! a = 1; | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
32 %! assert (a,1); |
5590 | 33 |
34 %!test | |
35 %! clear x; | |
36 %! try | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
37 %! clear a; |
5590 | 38 %! a; |
39 %! x = 1; | |
40 %! catch | |
41 %! end_try_catch | |
42 %! a = 2; | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
43 %! assert (!exist ('x')); |
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
44 %! assert (a,2); |
5590 | 45 |
46 %!test | |
47 %! try | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
48 %! clear a; |
5590 | 49 %! a; |
50 %! catch | |
51 %! x = 1; | |
52 %! end_try_catch | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
53 %! assert (exist ('x')); |
5590 | 54 |
55 %!test | |
56 %! try | |
57 %! clear a; | |
58 %! a; | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
59 %! error ("Shoudn't get here"); |
5590 | 60 %! catch |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
61 %! assert (lasterr()(1:13), "'a' undefined"); |
5590 | 62 %! end_try_catch |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
63 %! assert (lasterr()(1:13), "'a' undefined"); |
5590 | 64 |
16215
6fe6ac8bbfdb
test: Remove trailing spaces from ends of lines in test/ directory.
Rik <rik@octave.org>
parents:
16030
diff
changeset
|
65 %!test |
5590 | 66 %! try |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
67 %! error ("user-defined error"); |
5590 | 68 %! catch |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
69 %! assert (lasterr, "user-defined error"); |
5590 | 70 %! end_try_catch |
71 | |
72 %!function ms = mangle (s) | |
73 %! ## Wrap angle brackets around S. | |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16937
diff
changeset
|
74 %! ms = ["<" s ">"]; |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
75 %!endfunction |
5590 | 76 %!test |
77 %! try | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
78 %! clear a; |
5590 | 79 %! a; |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
80 %! error ("Shoudn't get here"); |
5590 | 81 %! catch |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
82 %! assert (mangle (lasterr)(1:14), "<'a' undefined"); |
5590 | 83 %! end_try_catch |
84 | |
85 %!test | |
86 %! try | |
87 %! try | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
88 %! clear a; |
5590 | 89 %! a; |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
90 %! error ("Shoudn't get here"); |
5590 | 91 %! catch |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
92 %! assert (lasterr()(1:13), "'a' undefined"); |
5590 | 93 %! end_try_catch |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
94 %! clear b; |
5590 | 95 %! b; |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
96 %! error ("Shoudn't get here"); |
5590 | 97 %! catch |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
98 %! assert (lasterr()(1:13), "'b' undefined"); |
5590 | 99 %! end_try_catch |
100 | |
101 %!test | |
102 %! try | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
103 %! clear a; |
5590 | 104 %! a; |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
105 %! error ("Shoudn't get here"); |
5590 | 106 %! catch |
107 %! try | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
108 %! assert (lasterr()(1:13), "'a' undefined"); |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
109 %! clear b; |
5590 | 110 %! b; |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
111 %! error ("Shoudn't get here"); |
5590 | 112 %! catch |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
113 %! assert (lasterr()(1:13), "'b' undefined"); |
5590 | 114 %! end_try_catch |
115 %! end_try_catch | |
116 | |
117 %!test | |
118 %! try | |
119 %! try | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
120 %! clear a; |
5590 | 121 %! a; |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
122 %! error ("Shoudn't get here"); |
5590 | 123 %! catch |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16937
diff
changeset
|
124 %! error (["rethrow: " lasterr]); |
5590 | 125 %! end_try_catch |
126 %! catch | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
127 %! assert (lasterr()(1:22), "rethrow: 'a' undefined"); |
5590 | 128 %! end_try_catch |
129 | |
17249
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
130 %!test |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
131 %! clear myerr; |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
132 %! try |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
133 %! error ("user-defined error"); |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
134 %! catch myerr |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
135 %! assert (myerr.message, "user-defined error"); |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
136 %! end_try_catch |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
137 |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
138 %!test |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
139 %! try |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
140 %! clear a; |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
141 %! error ("user-defined error"); |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
142 %! catch a=1; |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
143 %! assert (lasterr, "user-defined error"); |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
144 %! assert (a, 1); |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
145 %! end_try_catch |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
146 |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
147 %!test |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
148 %! clear myerr1 |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
149 %! clear myerr2 |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
150 %! try |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
151 %! try |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
152 %! clear a; |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
153 %! a; |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
154 %! catch myerr1 |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
155 %! error (myerr1); |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
156 %! end_try_catch |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
157 %! catch myerr2 |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
158 %! assert (myerr1.message, myerr2.message); |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
159 %! assert (myerr1.identifier, myerr2.identifier); |
923ce8b42db2
improve try-catch-statement to save exception to a variable (bug #33217)
Stefan Mahr <dac922@gmx.de>
parents:
16994
diff
changeset
|
160 %! end_try_catch |