Anonymous user
Assertions: Difference between revisions
JavaScript added
(Added BASIC256) |
(JavaScript added) |
||
Line 824:
Note: assertion checking is disabled by default when you run your program with the <tt>java</tt> command. You must provide the <tt>-ea</tt> (short for <tt>-enableassertions</tt>) flag in order to enable them.
=={{header|JavaScript}}==
<lang javaScript>
function check() {
try {
if (isNaN(answer)) throw '$answer is not a number';
if (answer != 42) throw '$answer is not 42';
}
catch(err) {
console.log(err);
answer = 42;
}
finally { console.log(answer); }
}
console.count('try'); // 1
let answer;
check();
console.count('try'); // 2
answer = 'fourty two';
check();
console.count('try'); // 3
answer = 23;
check();
</lang>
{{out}}<pre>
try: 1
$answer is not a number
42
try: 2
$answer is not a number
42
try: 3
$answer is not 42
42
</pre>
=={{header|Julia}}==
|