我读到Haskell实际上是运行时错误,尽管它是静态类型和功能的。但是,没有人说这可能是什么运行时错误。有人知道吗
最佳答案
在Control.Exception和GHC.Exception中定义的标准库(base
包)可能抛出所有运行时异常。error
是GHC.Err(基于GHC.Exception)中定义的函数,
error :: [Char] -> a
error s = raise# (errorCallException s)
如果未被某些处理程序捕获,它将抛出
ErrorCall
异常并向stderr打印错误消息,base
中纯函数引发的大多数运行时异常均由error
实现。一些例子:
undefined
(尚待实现的代码的占位符)定义为undefined :: a
undefined = error "undefined"
由于它的类型,它将通过编译步骤,并且在运行时对其进行评估时将引发异常。
GHC标准库出于历史原因导出了一些部分功能,例如
head
:head :: [a] -> a
head (x:_) = x
head [] = badHead
badHead :: a
badHead = errorEmptyList "head"
errorEmptyList :: String -> a
errorEmptyList fun =
error (prel_list_str ++ fun ++ ": empty list")
IOException
总结了您可能在其他编程语言中看到的大多数普通IO异常,例如FileNotFound,NoPermission,UnexpectedEOF等。它在 System.IO.Error
中进一步扩展,仅在IO monad上下文中引发。base
中有六个算术异常,分别是data ArithException
= Overflow
| Underflow
| LossOfPrecision
| DivideByZero
| Denormal
| RatioZeroDenominator
两种数组访问异常:
data ArrayException
= IndexOutOfBounds String
| UndefinedElement String
四个异步异常,即旨在在进程之间传递的异常,它们是:
data AsyncException
= StackOverflow
| HeapOverflow
| ThreadKilled
| UserInterrupt
当计算显然不会终止时:
NonTermination
当一个或多个进程被永久阻止时:
BlockedIndefinitelyOnMVar
, Deadlock
等。模式匹配失败时(主要在单子(monad)中):
PatternMatchFail
断言失败时:
AssertionFailed
还有更多。
关于haskell - Haskell会出现哪些类型的运行时错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34848154/