September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -7,4 +7,7 @@
This task is to give an example of an exception handling routine
and to "throw" a new exception.
Cf. [[Exceptions Through Nested Calls]]
;Related task:
*   [[Exceptions Through Nested Calls]]
<br><br>

View file

@ -1,9 +0,0 @@
FOOOBJECTNEW foo instance := foo base; # exception x routines are specific to a foo #
FOOMEND prior foo exception x mend = foo exception x mended OF foo;
on foo exception x(foo instance, raise foo exception x);
WHILE TRUE DO
# now manually raise example "foo exception x" #
raise foo exception x(foo instance)
OD;

View file

@ -1,8 +0,0 @@
on foo exception x(foo instance, prior foo exception x mend);
... EXIT
except foo exception x:
SKIP # exception x caught code here ... #
# to continue to propagate an exception x is a bit more problematic...#
# and requires nesting/restoring prior "raise foo exception x"#
on foo exception x(foo instance, prior foo exception x mend);
raise foo exception x(foo instance)

View file

@ -0,0 +1,5 @@
class MyException :: Exception
{
constructor new
<= new literal:"MyException raised".
}

View file

@ -0,0 +1,4 @@
foo
[
MyException new; raise.
]

View file

@ -0,0 +1,7 @@
try(o foo)
{
on(MyException e)
[
// handle exceptions of type MyException and derived
]
}

View file

@ -0,0 +1,4 @@
o foo | if(:e)
[
// handle any type of exception not handled by above catches
].

View file

@ -0,0 +1,30 @@
Public Sub Main()
Dim iInteger As Integer
MakeError
DivError
iInteger = "2.54"
Catch
Print Error.Text
End
'______________________
Public Sub DivError()
Print 10 / 0
Catch
Print Error.Text
End
'______________________
Public Sub MakeError()
Error.Raise("My Error")
Catch
Print Error.Text
End

View file

@ -0,0 +1,23 @@
// version 1.0.6
// In Kotlin all Exception classes derive from Throwable and, by convention, end with the word 'Exception'
class MyException (override val message: String?): Throwable(message)
fun foo() {
throw MyException("Bad foo!")
}
fun goo() {
try {
foo()
}
catch (me: MyException) {
println("Caught MyException due to '${me.message}'")
println("\nThe stack trace is:\n")
me.printStackTrace()
}
}
fun main(args: Array<String>) {
goo()
}

View file

@ -1,23 +0,0 @@
:- object(exceptions).
:- public(double/2).
double(X, Y) :-
catch(double_it(X,Y), Error, handler(Error, Y)).
handler(error(not_a_number(X), logtalk(This::double(X,Y), Sender)), Y) :-
% try to fix the error and resume computation;
% if not possible, rethrow the exception
( catch(number_codes(Nx, X), _, fail) ->
double_it(Nx, Y)
; throw(error(not_a_number(X), logtalk(This::double(X,Y), Sender)))
).
double_it(X, Y) :-
( number(X) ->
Y is 2*X
; this(This),
sender(Sender),
throw(error(not_a_number(X), logtalk(This::double(X,Y), Sender)))
).
:- end_object.

View file

@ -1,10 +0,0 @@
| ?- exceptions::double(1, Double).
Double = 2
yes
| ?- exceptions::double("1", Double).
Double = 2
yes
| ?- exceptions::double(a, Double).
uncaught exception: error(not_a_number(a),logtalk(exceptions::double(a,_),user))

View file

@ -0,0 +1,8 @@
errorproc:=proc(n)
local a;
try
a:=1/n;
catch "numeric exception: division by zero":
error "Something went wrong when dividing."
end try;
end proc;

View file

@ -0,0 +1,3 @@
throw("oh no")
throw(1)
throw(501,{"she",made[me],Do(it)})

View file

@ -0,0 +1,9 @@
try
one_of(these)
catch e
if e[E_CODE]=501 then
puts(1,"that's no excuse!\n")
else
throw(e)
end if
end try

View file

@ -0,0 +1 @@
throw "Any error message."

View file

@ -0,0 +1 @@
throw [System.IO.FileNotFoundException] ".\temp.txt not found."

View file

@ -0,0 +1,12 @@
try
{
Get-Content -Path .\temp.txt
}
catch [System.IO.FileNotFoundException]
{
Write-Host "File not found exception"
}
catch [System.Exception]
{
Write-Host "Other exception"
}

View file

@ -0,0 +1 @@
$Error[0] | Get-Member

View file

@ -1,15 +1,13 @@
/*REXX program demonstrates handling an exception; catching is via a label. */
/*REXX program demonstrates handling an exception (negative #); catching is via a label.*/
do j=9 by -5
say 'square root of' j "is" sqrt(j)
say 'the square root of ' j " is " sqrt(j)
end /*j*/
exit /*stick a fork in it, we're all done. */
.sqrtNeg: say 'illegal SQRT argument (argument is negative):' x
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9
numeric digits 9; numeric form; h=d+6; 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) /*make complex if X < 0.*/
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

View file

@ -0,0 +1,10 @@
capture confirm file titanium.dta
if _rc {
if _rc==601 {
display "the file does not exist"
}
else {
* all other cases
display "there was an error with return code " _rc
}
}

View file

@ -0,0 +1,4 @@
try{ throw(Exception.BadDay) }
catch { println(__exception," was thrown") }
fallthrough { println("No exception was thrown") }
println("OK");

View file

@ -0,0 +1,11 @@
fcn f(b){
try{
onExitBlock("Exit code".println);
if (b) throw(Exception.BadDay)
}
catch{ println(__exception," was thrown") }
fallthrough{ println("No exception was thrown") }
println("OK");
}
f(False); println("--------");
f(True);