September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
@ -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)
|
||||
5
Task/Exceptions/Elena/exceptions-1.elena
Normal file
5
Task/Exceptions/Elena/exceptions-1.elena
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class MyException :: Exception
|
||||
{
|
||||
constructor new
|
||||
<= new literal:"MyException raised".
|
||||
}
|
||||
4
Task/Exceptions/Elena/exceptions-2.elena
Normal file
4
Task/Exceptions/Elena/exceptions-2.elena
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
foo
|
||||
[
|
||||
MyException new; raise.
|
||||
]
|
||||
7
Task/Exceptions/Elena/exceptions-3.elena
Normal file
7
Task/Exceptions/Elena/exceptions-3.elena
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
try(o foo)
|
||||
{
|
||||
on(MyException e)
|
||||
[
|
||||
// handle exceptions of type MyException and derived
|
||||
]
|
||||
}
|
||||
4
Task/Exceptions/Elena/exceptions-4.elena
Normal file
4
Task/Exceptions/Elena/exceptions-4.elena
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
o foo | if(:e)
|
||||
[
|
||||
// handle any type of exception not handled by above catches
|
||||
].
|
||||
30
Task/Exceptions/Gambas/exceptions.gambas
Normal file
30
Task/Exceptions/Gambas/exceptions.gambas
Normal 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
|
||||
23
Task/Exceptions/Kotlin/exceptions.kotlin
Normal file
23
Task/Exceptions/Kotlin/exceptions.kotlin
Normal 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()
|
||||
}
|
||||
|
|
@ -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.
|
||||
|
|
@ -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))
|
||||
8
Task/Exceptions/Maple/exceptions.maple
Normal file
8
Task/Exceptions/Maple/exceptions.maple
Normal 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;
|
||||
3
Task/Exceptions/Phix/exceptions-1.phix
Normal file
3
Task/Exceptions/Phix/exceptions-1.phix
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
throw("oh no")
|
||||
throw(1)
|
||||
throw(501,{"she",made[me],Do(it)})
|
||||
9
Task/Exceptions/Phix/exceptions-2.phix
Normal file
9
Task/Exceptions/Phix/exceptions-2.phix
Normal 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
|
||||
1
Task/Exceptions/PowerShell/exceptions-1.psh
Normal file
1
Task/Exceptions/PowerShell/exceptions-1.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
throw "Any error message."
|
||||
1
Task/Exceptions/PowerShell/exceptions-2.psh
Normal file
1
Task/Exceptions/PowerShell/exceptions-2.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
throw [System.IO.FileNotFoundException] ".\temp.txt not found."
|
||||
12
Task/Exceptions/PowerShell/exceptions-3.psh
Normal file
12
Task/Exceptions/PowerShell/exceptions-3.psh
Normal 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"
|
||||
}
|
||||
1
Task/Exceptions/PowerShell/exceptions-4.psh
Normal file
1
Task/Exceptions/PowerShell/exceptions-4.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
$Error[0] | Get-Member
|
||||
|
|
@ -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
|
||||
|
|
|
|||
10
Task/Exceptions/Stata/exceptions.stata
Normal file
10
Task/Exceptions/Stata/exceptions.stata
Normal 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
|
||||
}
|
||||
}
|
||||
4
Task/Exceptions/Zkl/exceptions-1.zkl
Normal file
4
Task/Exceptions/Zkl/exceptions-1.zkl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
try{ throw(Exception.BadDay) }
|
||||
catch { println(__exception," was thrown") }
|
||||
fallthrough { println("No exception was thrown") }
|
||||
println("OK");
|
||||
11
Task/Exceptions/Zkl/exceptions-2.zkl
Normal file
11
Task/Exceptions/Zkl/exceptions-2.zkl
Normal 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);
|
||||
Loading…
Add table
Add a link
Reference in a new issue