unison/unison-src/transcripts/top-level-exceptions.output.md
Greg Pfeil 0031542faf
Add a space before code block info strings
This is for consistency with the `cmark` style. Now the blocks we still
pretty-print ourselves will match the bulk of them that `cmark`
produces.
2024-07-10 13:56:07 -06:00

2.0 KiB

A simple transcript to test the use of exceptions that bubble to the top level.

FYI, here are the Exception and Failure types:

scratch/main> view Exception Failure

  structural ability builtin.Exception where
    raise : Failure ->{builtin.Exception} x
  
  type builtin.io2.Failure
    = Failure Type Text Any

Here's a sample program just to verify that the typechecker allows run to throw exceptions:

use builtin IO Exception Test.Result

main : '{IO, Exception} ()
main _ = ()

mytest : '{IO, Exception} [Test.Result]
mytest _ = [Ok "Great"]

  Loading changes detected in scratch.u.

  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]

scratch/main> run main

  ()

scratch/main> add

  ⍟ I've added these definitions:
  
    main   : '{IO, Exception} ()
    mytest : '{IO, Exception} [Result]

scratch/main> io.test mytest

    New test results:
  
    1. mytest   ◉ Great
  
  ✅ 1 test(s) passing
  
  Tip: Use view 1 to view the source of a test.

Now a test to show the handling of uncaught exceptions:

main2 = '(error "oh noes!" ())

error : Text -> a ->{Exception} x
error msg a =
  builtin.Exception.raise (Failure (typeLink RuntimeError) msg (Any a))

unique type RuntimeError =

  Loading changes detected in scratch.u.

  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`:
    
      type RuntimeError
      error : Text -> a ->{Exception} x
      main2 : '{Exception} r

scratch/main> run main2

  💔💥
  
  The program halted with an unhandled exception:
  
    Failure (typeLink RuntimeError) "oh noes!" (Any ())
  
  Stack trace:
    ##raise