Monday, April 14, 2014

Haskell Week 8

Exceptions:

Exceptions and the way we handle them in Haskell are very similar to the methods we use in Java. Exceptions can be thrown, caught, and handled.

Some of the most common exceptions in Haskell include:

isAlreadyExistsError
isDoesNotExistError
isAlreadyInUseError
isFullError
isEOFError
isIllegalOperation
isPermissionError
isUserError

I find that the error reporting is much more readable and helpful in Haskell then in some of the other languages we have looked at, such as Java, VB, C#, and Python.

Here is a little error that I was given when I tried to run a method without creating an instance of anything to be returned to the console:

<interactive>:22:1:
    No instance for (Fractional [t0]) arising from a use of `myDiv1'
    Possible fix: add an instance declaration for (Fractional [t0])
    In the expression: myDiv1 50 [1, 2, 5, 0, ....]
    In an equation for `it': it = myDiv1 50 [1, 2, 5, ....]

<interactive>:22:8:
    No instance for (Num [t0]) arising from the literal `50'
    Possible fix: add an instance declaration for (Num [t0])
    In the first argument of `myDiv1', namely `50'
    In the expression: myDiv1 50 [1, 2, 5, 0, ....]
    In an equation for `it': it = myDiv1 50 [1, 2, 5, ....]

<interactive>:22:12:
    No instance for (Num t0) arising from the literal `1'
    The type variable `t0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there are several potential instances:
      instance Num Double -- Defined in `GHC.Float'
      instance Num Float -- Defined in `GHC.Float'
      instance Integral a => Num (GHC.Real.Ratio a)
        -- Defined in `GHC.Real'
      ...plus three others
    In the expression: 1
    In the second argument of `myDiv1', namely `[1, 2, 5, 0, ....]'
    In the expression: myDiv1 50 [1, 2, 5, 0, ....]


Not only is the error shown, but there are also possible solutions that are given to attempt to correct the error. I have found that these solutions are actually very helpful and often times lead to fixing the problem and producing working code.

No comments:

Post a Comment