Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,38 @@
|
|||
import Control.Monad.Error
|
||||
import Control.Monad.Trans (lift)
|
||||
|
||||
-- Our "user-defined exception" tpe
|
||||
data MyError = U0 | U1 | Other deriving (Eq, Read, Show)
|
||||
|
||||
-- Required for any error type
|
||||
instance Error MyError where
|
||||
noMsg = Other
|
||||
strMsg _ = Other
|
||||
|
||||
-- Throwing and catching exceptions implies that we are working in a monad. In
|
||||
-- this case, we use ErrorT to support our user-defined exceptions, wrapping
|
||||
-- IO to be able to report the happenings. ('lift' converts ErrorT e IO a
|
||||
-- actions into IO a actions.)
|
||||
|
||||
foo = do lift (putStrLn "foo")
|
||||
mapM_ (\toThrow -> bar toThrow -- the protected call
|
||||
`catchError` \caught -> -- the catch operation
|
||||
-- ↓ what to do with it
|
||||
case caught of U0 -> lift (putStrLn "foo caught U0")
|
||||
_ -> throwError caught)
|
||||
[U0, U1] -- the two exceptions to throw
|
||||
|
||||
|
||||
bar toThrow = do lift (putStrLn " bar")
|
||||
baz toThrow
|
||||
|
||||
baz toThrow = do lift (putStrLn " baz")
|
||||
throwError toThrow
|
||||
|
||||
-- We cannot use exceptions without at some outer level choosing what to do
|
||||
-- if an exception propagates all the way up. Here we just print the exception
|
||||
-- if there was one.
|
||||
main = do result <- runErrorT foo
|
||||
case result of
|
||||
Left e -> putStrLn ("Caught error at top level: " ++ show e)
|
||||
Right v -> putStrLn ("Return value: " ++ show v)
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
import Exceptions
|
||||
|
||||
class U0 : Exception()
|
||||
method getMessage()
|
||||
return "U0: " || (\message | "unknown")
|
||||
end
|
||||
end
|
||||
|
||||
class U1 : Exception()
|
||||
method getMessage()
|
||||
return "U1: " || (\message | "unknown")
|
||||
end
|
||||
end
|
||||
|
||||
procedure main()
|
||||
# (Because Exceptions are not built into Unicon, uncaught
|
||||
# exceptions are ignored. This clause will catch any
|
||||
# exceptions not caught farther down in the code.)
|
||||
case Try().call{ foo() } of {
|
||||
Try().catch(): {
|
||||
ex := Try().getException()
|
||||
write(ex.getMessage(), ":\n", ex.getLocation())
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
procedure foo()
|
||||
every 1|2 do {
|
||||
case Try().call{ bar() } of {
|
||||
Try().catch("U0"): {
|
||||
ex := Try().getException()
|
||||
write(ex.getMessage(), ":\n", ex.getLocation())
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
procedure bar()
|
||||
return baz()
|
||||
end
|
||||
|
||||
procedure baz()
|
||||
initial U0().throw("First exception")
|
||||
U1().throw("Second exception")
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue