首页 > 解决方案 > 变量不在范围内:catch

问题描述

我有这个函数可以用这个代码打开一个文件,但它正在返回

teste.hs: 228: 5: error:
    Variable not in scope:
      catch :: IO Handle -> (t0 -> IO Handle) -> IO Handle

我的代码:

opensArchive :: String -> IOMode -> IO Handle
opensFile file mode =
    catch (openFile mode file)
       (\ _ -> do {
     putStrLn ("Unable to open" ++ file);
     putStrLn "It will be opened with a default name: data.txt and cleared";
     pt_arq <- opensFile "data.txt" WriteMode;
     closeArchive pt_arq;
     opensFile "data.txt" ReadMode
      }
    )

closeArchive :: Handle -> IO ()
closeFile handle_arq = hClose handle_arq

Haskell文件打开功能

标签: haskell

解决方案


您可能想查看 function 的规范catch

冲浪到Hoogle。然后在网页左上角的搜索栏中输入“catch”。

然后单击顶部解决方案。这将使您进入function的规范网页catch

在该页面的最顶部,您会看到它是关于 moduleControl.Exception的。你导入了吗?

测试下ghci

$ ghci
GHCi, version 8.6.5: http://www.haskell.org/ghc/  :? for help
...
 λ> 
 λ> :type catch

 <interactive>:1:1: error: Variable not in scope: catch
 λ> 
 λ> import Control.Exception
 λ> 
 λ> :type catch
 catch :: Exception e => IO a -> (e -> IO a) -> IO a
 λ> 
 λ> 

瞧!


推荐阅读