This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,4 @@
"Install Linux, Problem Solved" throw
TUPLE: velociraptor ;
\ velociraptor new throw

View file

@ -0,0 +1,2 @@
ERROR: velociraptor ;
velociraptor

View file

@ -0,0 +1,12 @@
! Preferred exception handling
: try-foo
[ foo ] [ foo-failed ] recover ;
: try-bar
[ bar ] [ bar-errored ] [ bar-always ] cleanup ;
! Used rarely
[ "Fail" throw ] try ! throws a "Fail"
[ "Fail" throw ] catch ! returns "Fail"
[ "Hi" print ] catch ! returns f (looks the same as throwing f; don't throw f)
[ f throw ] catch ! returns f, bad! use recover or cleanup instead

View file

@ -0,0 +1,20 @@
# define custom exception class
# StandardError is base class for all exception classes
class MyError : StandardError {
def initialize: message {
# forward to StdError's initialize method
super initialize: message
}
}
try {
# raises/throws a new MyError exception within try-block
MyError new: "my message" . raise!
} catch MyError => e {
# catch exception
# this will print "my message"
e message println
} finally {
# this will always be executed (as in e.g. Java)
"This is how exception handling in Fancy works :)" println
}

View file

@ -0,0 +1,23 @@
// Create a new error class by subclassing sys::Err
const class SpecialErr : Err
{
// you must provide some message about the error
// to the parent class, for reporting
new make () : super ("special error") {}
}
class Main
{
static Void fn ()
{
throw SpecialErr ()
}
public static Void main ()
{
try
fn()
catch (SpecialErr e)
echo ("Caught " + e)
}
}

View file

@ -0,0 +1,18 @@
pickyPicky =: verb define
if. y-:'bad argument' do.
throw.
else.
'thanks!'
end.
)
tryThis =: verb define
try.
pickyPicky y
catcht.
'Uh oh!'
end.
)
tryThis 'bad argument'
Uh oh!

View file

@ -0,0 +1,7 @@
to div.checked :a :b
if :b = 0 [(throw "divzero 0)]
output :a / :b
end
to div.safely :a :b
output catch "divzero [div.checked :a :b]
end

View file

@ -0,0 +1 @@
raise(E_PERM);

View file

@ -0,0 +1,5 @@
try
this:foo();
except e (ANY)
this:bar(e);
endtry

View file

@ -0,0 +1,5 @@
try
this:foo();
finally
this:bar();
endtry

View file

@ -0,0 +1 @@
`this:foo()!ANY=>this:bar()';

View file

@ -0,0 +1,2 @@
all:
false

View file

@ -0,0 +1,2 @@
all:
-@make -f fail.mk

View file

@ -0,0 +1,2 @@
all:
make -f fail.mk; exit 0

View file

@ -0,0 +1,8 @@
f[x_] := If[x > 10, Throw[overflow], x!]
Example usage :
Catch[f[2] + f[11]]
-> overflow
Catch[f[2] + f[3]]
-> 8

View file

@ -0,0 +1,2 @@
EXCEPTION EndOfFile;
EXCEPTION Error(TEXT);

View file

@ -0,0 +1,4 @@
PROCEDURE Foo() RAISES { EndOfFile } =
...
RAISE EndOfFile;
...

View file

@ -0,0 +1,5 @@
TRY
Foo();
EXCEPT
| EndOfFile => HandleFoo();
END;

View file

@ -0,0 +1,5 @@
TRY
Foo();
FINALLY
CleanupFoo(); (* always executed *)
END;