Haskell 输入'if';readFile的in-catch子句

Haskell 输入'if';readFile的in-catch子句,haskell,exception-handling,Haskell,Exception Handling,当我读取一个文件时,我正在尝试处理一个异常,但我收到了消息 输入'if'时分析错误 readTxt=do {catch(read_文件)修复_错误;} 哪里 read_file=do { f在fix\u error的定义中,您交换了=和if 定义的语法为: name args = body if表达式的语法为: if condition then true_branch else false_branch 您只需结合以下几点: fix_error erro = if isDoesNotExi

当我读取一个文件时,我正在尝试处理一个异常,但我收到了消息

输入'if'时分析错误

readTxt=do
{catch(read_文件)修复_错误;}
哪里
read_file=do
{

f在
fix\u error
的定义中,您交换了
=
if

定义的语法为:

name args = body
if
表达式的语法为:

if condition then true_branch else false_branch
您只需结合以下几点:

fix_error erro = if isDoesNotExistError erro
               ----
  then do
    putStr "Exception: file doesn't exist"
    ...
  else ioError erro
或使用防护装置:

fix_error erro
  | isDoesNotExistError erro = do
    putStr "Exception: file doesn't exist"
    ...
  | otherwise = ioError erro

我省略了大括号和分号,但如果您愿意,也可以包含它们。

我认为if和等号应该互换位置​
fix_error erro
  | isDoesNotExistError erro = do
    putStr "Exception: file doesn't exist"
    ...
  | otherwise = ioError erro