2021-05-27 17:06:19 +03:00
|
|
|
A simple transcript to test the use of exceptions that bubble to the top level.
|
|
|
|
|
|
|
|
FYI, here are the `Exception` and `Failure` types:
|
|
|
|
|
2024-07-10 19:17:15 +03:00
|
|
|
``` ucm
|
2024-06-12 01:22:09 +03:00
|
|
|
scratch/main> view Exception Failure
|
2021-05-27 17:06:19 +03:00
|
|
|
|
2021-08-24 00:05:37 +03:00
|
|
|
structural ability builtin.Exception where
|
2021-05-27 17:06:19 +03:00
|
|
|
raise : Failure ->{builtin.Exception} x
|
|
|
|
|
2024-01-04 11:42:12 +03:00
|
|
|
type builtin.io2.Failure
|
2021-05-27 17:06:19 +03:00
|
|
|
= Failure Type Text Any
|
|
|
|
|
|
|
|
```
|
|
|
|
Here's a sample program just to verify that the typechecker allows `run` to throw exceptions:
|
|
|
|
|
2024-07-10 17:46:18 +03:00
|
|
|
``` unison
|
2021-05-27 17:06:19 +03:00
|
|
|
use builtin IO Exception Test.Result
|
|
|
|
|
|
|
|
main : '{IO, Exception} ()
|
|
|
|
main _ = ()
|
|
|
|
|
|
|
|
mytest : '{IO, Exception} [Test.Result]
|
|
|
|
mytest _ = [Ok "Great"]
|
|
|
|
```
|
|
|
|
|
2024-07-10 19:17:15 +03:00
|
|
|
``` ucm
|
2021-05-27 17:06:19 +03:00
|
|
|
|
2023-12-22 14:55:24 +03:00
|
|
|
Loading changes detected in scratch.u.
|
|
|
|
|
2021-05-27 17:06:19 +03:00
|
|
|
I found and typechecked these definitions in scratch.u. If you
|
|
|
|
do an `add` or `update`, here's how your codebase would
|
|
|
|
change:
|
|
|
|
|
|
|
|
⍟ These new definitions are ok to `add`:
|
|
|
|
|
|
|
|
main : '{IO, Exception} ()
|
|
|
|
mytest : '{IO, Exception} [Result]
|
|
|
|
|
|
|
|
```
|
2024-07-10 19:17:15 +03:00
|
|
|
``` ucm
|
2024-06-12 01:22:09 +03:00
|
|
|
scratch/main> run main
|
2021-05-27 17:06:19 +03:00
|
|
|
|
2022-08-15 21:30:14 +03:00
|
|
|
()
|
|
|
|
|
2024-06-12 01:22:09 +03:00
|
|
|
scratch/main> add
|
2021-05-27 17:06:19 +03:00
|
|
|
|
|
|
|
⍟ I've added these definitions:
|
|
|
|
|
|
|
|
main : '{IO, Exception} ()
|
|
|
|
mytest : '{IO, Exception} [Result]
|
|
|
|
|
2024-06-12 01:22:09 +03:00
|
|
|
scratch/main> io.test mytest
|
2021-05-27 17:06:19 +03:00
|
|
|
|
|
|
|
New test results:
|
|
|
|
|
2024-06-27 08:14:37 +03:00
|
|
|
1. mytest ◉ Great
|
2021-05-27 17:06:19 +03:00
|
|
|
|
|
|
|
✅ 1 test(s) passing
|
|
|
|
|
2024-06-26 23:01:36 +03:00
|
|
|
Tip: Use view 1 to view the source of a test.
|
2021-05-27 17:06:19 +03:00
|
|
|
|
|
|
|
```
|
|
|
|
Now a test to show the handling of uncaught exceptions:
|
|
|
|
|
2024-07-10 17:46:18 +03:00
|
|
|
``` unison
|
2021-05-27 17:06:19 +03:00
|
|
|
main2 = '(error "oh noes!" ())
|
|
|
|
|
|
|
|
error : Text -> a ->{Exception} x
|
|
|
|
error msg a =
|
|
|
|
builtin.Exception.raise (Failure (typeLink RuntimeError) msg (Any a))
|
|
|
|
|
|
|
|
unique type RuntimeError =
|
|
|
|
```
|
|
|
|
|
2024-07-10 19:17:15 +03:00
|
|
|
``` ucm
|
2021-05-27 17:06:19 +03:00
|
|
|
|
2023-12-22 14:55:24 +03:00
|
|
|
Loading changes detected in scratch.u.
|
|
|
|
|
2021-05-27 17:06:19 +03:00
|
|
|
I found and typechecked these definitions in scratch.u. If you
|
|
|
|
do an `add` or `update`, here's how your codebase would
|
|
|
|
change:
|
|
|
|
|
|
|
|
⍟ These new definitions are ok to `add`:
|
|
|
|
|
2024-01-04 11:42:12 +03:00
|
|
|
type RuntimeError
|
2021-05-27 17:06:19 +03:00
|
|
|
error : Text -> a ->{Exception} x
|
|
|
|
main2 : '{Exception} r
|
|
|
|
|
|
|
|
```
|
2024-07-10 19:17:15 +03:00
|
|
|
``` ucm
|
2024-06-12 01:22:09 +03:00
|
|
|
scratch/main> run main2
|
2021-05-27 17:06:19 +03:00
|
|
|
|
2021-07-22 19:54:36 +03:00
|
|
|
💔💥
|
|
|
|
|
2021-07-26 21:43:37 +03:00
|
|
|
The program halted with an unhandled exception:
|
2021-07-22 19:54:36 +03:00
|
|
|
|
2022-02-13 08:20:06 +03:00
|
|
|
Failure (typeLink RuntimeError) "oh noes!" (Any ())
|
2024-04-16 21:26:28 +03:00
|
|
|
|
2022-10-20 23:21:15 +03:00
|
|
|
Stack trace:
|
|
|
|
##raise
|
2021-05-27 17:06:19 +03:00
|
|
|
|
|
|
|
```
|