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-ide-haskell-hoogle 是一个专门为 Atom 编辑器设计的插件,它整合了 Haskell 的 Hoogle 工具,以提供强大的代码提示和搜索功能。Atom 是一款由 GitHub 开发的开源文本编辑器,它利用现代 web 技术如 HTML、CSS ...
在 Emacs 中,`haskell-mode` 是一个专门为了提升 Haskell 开发体验而设计的模式。 `haskell-mode` 提供了多种增强功能,旨在帮助 Haskell 开发者更高效地编写、调试和理解代码。这个模式包括以下关键特性: 1. **...
在数据可视化领域,`haskell-chart`库提供了一种高效且灵活的方式来创建2D图表,这对于数据分析、科学计算以及教学等场景非常有用。这个库是开源的,意味着任何人都可以查看其源代码,学习并贡献改进。 `haskell-...
**哈斯克尔编程语言与Atom-Haskell-GHC-Mod** 哈斯克尔(Haskell)是一种纯函数式编程语言,以其优雅的语法、强静态类型系统和编译时优化而受到程序员的喜爱。它鼓励使用不可变数据和惰性求值,这使得哈斯克尔在...
Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码 Haskell-Data-Analysis-Cookbook这是 Haskell数据分析 cookbook的附带源代码。最新的源代码可以在GitHub上获得: ...
Atom-Haskell-Debug是针对Haskell开发者的一个强大工具,它允许你在流行的Atom文本编辑器中集成一个图形化的Haskell调试器。这个工具基于GHCi(Glasgow Haskell Compiler Interface),GHCi是Haskell的交互式环境,...
从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-英文版
【标题】:“Atom-atom-haskell-scry,扩散系数.zip” 涉及的主要知识点是 Atom 编辑器与 Haskell 语言的集成以及 SCRY 工具的使用。 【描述】:“Atom-atom-haskell-scry.zip”的描述指出,这个压缩包包含了一个名...
用于 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 ...
Atom-ide-haskell-repl是针对Atom文本编辑器的一个扩展插件,专为Haskell编程语言提供集成的GHCi(Glasgow Haskell Compiler Interface)交互式环境,即REPL(Read-Eval-Print Loop)。这个插件允许开发者在Atom编辑...
在Haskell的世界里,开发环境的配置至关重要,而`haskell-dev-tools`就是一个方便的元项目,它专门设计用于简化Haskell开发工具的安装和升级过程。这个项目扮演了一个集合和自动化工具的角色,使得开发者无需手动...
Haskell-dap是一个开源项目,它实现了调试适应性协议(Debug Adapter Protocol,简称DAP)的接口,使得Haskell开发者可以充分利用这个协议进行程序调试。DAP是一个通用的、跨平台的协议,允许IDEs(集成开发环境)和...
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 … ...
### 函数式编程:Haskell到Java的转换 #### 概述 本文旨在探讨函数式编程语言Haskell如何被编译或转换为Java语言。Haskell作为一种纯函数式编程语言,以其强大的类型系统、惰性求值机制以及高度抽象的能力在学术界...
"haskell-tools"就是这样一个项目,它专注于为Haskell开发者提供一系列实用的辅助工具,以优化他们的开发流程。 ### 1. GHC:Glasgow Haskell Compiler GHC是Haskell的主要编译器,也是haskell-tools的重要组成...
Atom-ide-haskell-cabal.zip,Cabal backend provider for ide-haskellIDE Haskell Cabal套餐,atom是一个用web技术构建的开源文本编辑器。
### Haskell - The Craft of Functional Programming #### 一、概述 《Haskell - The Craft of Functional Programming》是一本深入探讨Haskell编程语言的经典著作。本书由Tantanoid编写,并于1999年由Addison-...
你可以在找到 haskell-brainfuck用法图书馆 import HaskBF.Evalimport qualified Data.ByteString.Lazy as BSimport Control.Monad.Statemain = do -- The following will evaluate the file using stdin and ...