you might be wondering why the exceptions are in the input and outptu chapter, is there a fit, actually, the resaons why the haskell appears in the input and output chapter has its own reason.
Haskell has different philosohpies from other programming languages where it does not necessary use the exception to represent some error conditions. Language like C uses return value to signal success or failure, while haskell has type such as Maybe, where it can return a meaning value if the everything is all right, and it can return a nohting to signify a failure condition.
Despite having expressive types that support failed computations, Haskell still has support for exceptions, because they make more sense in I/O contexts. A lot of things can go wrong when dealing with the outside world because it is so unreliable. For instance, when opening a file, a bunch of things can go wrong. The file might be locked, it might not be there at all or the hard disk drive or something might not be there at all. So it's good to be able to jump to some error handling part of our code when such an error occurs.
so besides I/O, the impure code can throw exceptions, for pure code such as divide by zero is a very common error condition. getting the head of a empty list will throw an exception.
ghci> 4 `div` 0 *** Exception: divide by zero ghci> head [] *** Exception: Prelude.head: empty list
so to refine our statement,
ure code can throw exceptions, but it they can only be caught in the I/O part of our code (when we're inside a do block that goes into main). That's because you don't know when (or if) anything will be evaluated in pure code, because it is lazy and doesn't have a well-defined order of execution, whereas I/O code does.
- we will start the discussion beginning with the an doesFileExists exception and
- an example with doesFileExists exception
- try-catch handler in haskell
- some guideline on whether to choose exception over Maybe values
-- file -- exception101.hs -- descrpition: -- I/O code (i.e. impure code) can throw exception -- -- 4 `div` 0 -- head [] -- Pure code can throw exceptions, but it they can only be caught in the I/O part of our code (when we're inside a do block that goes into main). -- QUOTE: -- Pure code can throw exceptions, but it they can only be caught in the I/O part of our code (when we're inside a do block that goes into main). That's because you don't know when (or if) anything will be evaluated in pure code, because it is lazy and doesn't have a well-defined order of execution, whereas I/O code does. -- QUOTE: -- The logic of our program should reside mostly within our pure functions, because their results are dependant only on the parameters that the functions are called with. When dealing with pure functions, you only have to think about what a function returns, because it can't do anything else. -- QUOTE: -- Pure functions are lazy by default, which means that we don't know when they will be evaluated and that it really shouldn't matter. -- However, once pure functions start throwing exceptions, it matters when they are evaluated. That's why we can only catch exceptions thrown from pure functions in the I/O part of our code. And that's bad, because we want to keep the I/O part as small as possible. However, if we don't catch them in the I/O part of our code, our program crashes. The solution? Don't mix exceptions and pure code. import System.Environment import System.IO -- main = do (fileName : _) <- getArgs -- contents <- readFile fileName -- putStrLn $ "The file has " ++ show (length (lines contents)) ++ "lines!" -- throws exception when file with the filename provided does not exists -- solution I : check before act -- doesFileExist -- import System.Environment -- import System.IO -- import System.Directory -- -- main = do (fileName:_) <- getArgs -- fileExists <- doesFileExist fileName -- if fileExists -- then do contents <- readFile fileName -- putStrLn $ "The file has " ++ show (length (lines contents)) ++ " lines!" -- else do putStrLn "The file doesn't exist!"
and the try-catch handler in examples.
-- file -- ioeGetFileName.hs -- descrpition: -- ioe functions families to let you ask the exceptions some attributes -- import System.Environment import System.IO import System.IO.Error main = toTry `catch` handler toTry :: IO () toTry = do (fileName:_) <- getArgs contents <- readFile fileName putStrLn $ "The file has " ++ show (length (lines contents)) ++ " lines!" handler :: IOError -> IO () handler e | isDoesNotExistError e = case ioeGetFileName e of Just path -> putStrLn $ "Whoops! File does not exist at: " ++ path Nothing -> putStrLn "Whoops! File does not exist at unknown location!" | otherwise = ioError e -- or you can add hook more than one handler to the functions -- main = do toTry `catch` handler1 -- thenTryThis `catch` handler2 -- launchRockets -- -- This is kind of similar to try-catch blocks of other languages -- I prefer to have their type be something like IO (Either a b), meaning that they're normal I/O actions but the result that they yield when performed is of type Either a b, meaning it's either Left a or Right b.
last, let's see the exception hierarchy that you might be able to use i haskel.
-- file -- exception_trycatch.hs -- descrpition: -- cehck to see if the exception is the one that we are expecting -- import System.Environment import System.IO import System.IO.Error main = toTry `catch` handler toTry :: IO () toTry = do (fileName:_) <- getArgs contents <- readFile fileName putStrLn $ "The file has " ++ show (length (lines contents)) ++ " lines!" handler :: IOError -> IO () handler e | isDoesNotExistError e = putStrLn "The file doesn't exist!" | otherwise = ioError e -- the following types of exception that exists below -- isAlreadyExistsError -- isDoesNotExistError -- isAlreadyInUseError -- isFullError -- isEOFError -- isIllegalOperation -- isPermissionError -- isUserError -- so that you could have a handlers that looks like something like this: -- handler :: IOError -> IO () -- handler e -- | isDoesNotExistError e = putStrLn "The file doesn't exist!" -- | isFullError e = freeSomeSpace -- | isIllegalOperation e = notifyCops -- | otherwise = ioError e -- there a series of exception that exists which start with ioe and you can see a full list of them -- such as ioeGetFileName -- -- ioeGetFileName :: IOError -> Maybe FilePath
相关推荐
Atom-haskell-ghc-mod.zip,haskell-ghc-mod atom packagehaskell ghc mod atom包,atom是一个用web技术构建的开源文本编辑器。
haskell-mode emacs haskell-mode emacs
Atom-ide-haskell-hoogle.zip,在光标下显示符号的滚动信息艾德·哈斯克尔·胡格尔,atom是一个用web技术构建的开源文本编辑器。
Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码 Haskell-Data-Analysis-Cookbook这是 Haskell数据分析 cookbook的附带源代码。最新的源代码可以在GitHub上获得: ...
Programming-in-Haskell-2nd-Edition.pdf
从1.0.0开始,haskell-ghc-mod提供haskell-completion-backend服务。 注意:在1.0.0之前,提供了ide-backend服务。 它已被废弃以支持ide-haskell的UPI。 您可以在找到描述 执照 版权所有:copyright:2015 Atom-...
haskell-ghc-mod原子包 该软件包主要用作后端。 Haskell ghc-mod打开通往ghc-modi的管道,并查询类型,信息并检查错误。 安装与配置 请参考官方文档站点 服务中心API 从1.0.0版本开始,haskell-ghc-mod提供...
Get Programming with HASKELL-2018-英文版
用于 haskell-relational-record 的 MySQL 驱动程序 这个项目被合并到 。 准备 $ git clone git@github.com:khibino/haskell-relational-record.git $ git clone git@github.com:bos/hdbc-mysql.git $ git clone ...
演示医疗用例的参考DAML应用程序 -Haskell-TypeScript-下载
haskell-chart, haskell的2D 图表库 图 haskell的2D 图表库进一步的信息可以在关联的 wiki中找到。
Server Metaprogramming Ruby-Pyton-Groovy-Haskell-Erlang.pdf
A History of Haskell - Being Lazy With Class
haskell-formatter --force --input a.hs --output a.hs hindent a.hs 就地格式化多个文件 hindent a.hs b.hs 将stdin格式化为stdout haskell-formatter hindent 订单进口 完成 hindent --sort-imports … ...
Atom-ide-haskell-cabal.zip,Cabal backend provider for ide-haskellIDE Haskell Cabal套餐,atom是一个用web技术构建的开源文本编辑器。
你可以在找到 haskell-brainfuck用法图书馆 import HaskBF.Evalimport qualified Data.ByteString.Lazy as BSimport Control.Monad.Statemain = do -- The following will evaluate the file using stdin and ...
haskell-lsp-client 该软件包适用于希望使其文本编辑器与兼容的文本编辑器的开发人员。 我已经开发了此软件包,并计划将其集成到。 示例客户端 该存储库中包含一个示例客户端。 此示例客户端仅运行并打开在命令行...
Atom-atom-haskell-scry.zip,De-emphasize qualified Haskell identifiers.SCRY,atom是一个用web技术构建的开源文本编辑器。
Atom-atom-haskell-pointfree.zip,atom包:将选择转换为无点或有点表示Haskell无点包,atom是一个用web技术构建的开源文本编辑器。
Haskell - The Craft of Functional Programming, 2ed (Addison-Wesley, 1999) by Tantanoid 已加书签