‘그냥 tryAny 쓰면 예외는 다 잡을 수 있는 거 아닌가? 왜 ResourceT를 써야 하지?’라고 생각했는데 찾아보니 tryAny로는 비동기 예외를 잡을 수 없다고 한다.

writeGreetingSafeAttempt :: IO ()
writeGreetingSafeAttempt = do
  dir <- getDataDir
  h <- IO.openFile (dir </> "greeting.txt") WriteMode
  _ <- tryAny do
    IO.hPutStrLn h "hello"
    IO.hPutStrLn h "world"
  IO.hClose h
4

If you have a fediverse account, you can quote this note from your own instance. Search https://hackers.pub/ap/notes/0196e2c9-30a0-7eea-afc8-938d3f300fee on your instance and quote it. (Note that quoting is not supported in Mastodon.)