[英]How to handle exceptions in Frege?
試圖處理異常我發現了一個相關的問題:
但是我不清楚如何使用try/catch/finally
表達式。
問題 :
我想讀取一個文件並返回其所有行。 如果它不存在,我可能想要返回一個空列表。 就像是:
getContent :: String -> IO [String]
getContent filePath = openReader filePath >>= \reader -> reader.getLines
`catch` (\(e::FileNotFoundException) -> return [])
`finally` (println "something went wrong")
之前的代碼編譯但在執行時它只顯示以下內容:
frege> getContent "asdf"
java.io.FileNotFoundException: asdf (No such file or directory)
問題:
docs/wiki/frege goodness
中的更多例子會有很多幫助。 謝謝
到目前為止,代碼看起來很好,但是lambda存在問題。 就像在Haskell中一樣,lambda在語法上盡可能地擴展。 因此,盡管catch
優先級較低,因為>>=
它仍然屬於lambda。
順便說一下,這種lambda表達式有一個簡寫形式:
_.foo
是一個荒謬的術語
\it -> it.foo
還可以應用額外的參數:
_.foo bar baz
得到了貶低
\it -> it.foo bar baz
這完全適用於上述情況。
在REPL中,您可以獲得有關catch的文檔,最后使用:help命令獲取>> =。
你是對的,這對弗雷格善良來說是一個很好的問題。 但是,github repo中也有一些工作示例。 對於這種情況,請查看examples / SimpleIO.fr
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.