RosettaCodeData/Task/Exceptions/REXX/exceptions.rexx

14 lines
1.1 KiB
Rexx
Raw Permalink Normal View History

2017-09-23 10:01:46 +02:00
/*REXX program demonstrates handling an exception (negative #); catching is via a label.*/
2015-11-18 06:14:39 +00:00
do j=9 by -5
2017-09-23 10:01:46 +02:00
say 'the square root of ' j " is " sqrt(j)
2013-06-05 21:47:54 +00:00
end /*j*/
2017-09-23 10:01:46 +02:00
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); h=d+6; m.=9
numeric digits; numeric form; if x<0 then signal .sqrtNeg
parse value format(x, 2, 1, , 0) 'E0' with g 'E' _ .; g=g * .5'e'_ % 2
do j=0 while h>9; m.j=h; h=h%2 + 1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g) * .5; end /*k*/
numeric digits d; return g/1
.sqrtNeg: say 'illegal SQRT argument (argument is negative):' x; exit 13