Trying to throw an exception in Haskell:
import Control.Exception
import Data.Typeable
data MyException = ThisException | ThatException deriving (Show, Typeable)
instance Exception MyException
data CellPos = CellPos Int Int deriving (Show, Read)
test :: String -> IO CellPos
test str = do
{
if length str == 0
then
throw ThisException;
else
return (CellPos 0 0);
}
Compiler says:
Can't make a derived instance of `Typeable MyException':
You need -XDeriveDataTypeable to derive an instance for this class
In the data type declaration for `MyException'
How I can fix it?
Could you also write how I can catch such exception when calling test function?
You’re getting this error because you’re trying to derive the an instance for the Typeable class for your data type (with
deriving (Show, Typeable); a Typeable instance is required for exception types), but this isn’t possible in standard Haskell; you need a GHC extension to do it.You could write a Typeable instance manually, but using
DeriveDataTypeableis actually the recommended way to do this. To enable the extension, you can put:at the top of your source file. Passing
-XDeriveDataTypeableon the command-line also works, but isn’t recommended; it’s best to document the language extensions you use at the top of the file, and it also simplifies compilation, as you don’t have to remember the flags. (It also isolates the extensions to the files that need them.)Also, you should replace
throwwiththrowIOin the definition oftest, as it guarantees the correct ordering in the IO monad.You should also add
above your imports, as the Prelude’s
catchis for an older exception-handling mechanism that would otherwise clash with Control.Exception when you try and catch an exception.Catching an exception is simple; you just use
catch:(The
foo `catch` barsyntax is the same ascatch foo bar; it works for any function.)Note that your exception handler has to have the same return type as the action you’re running; you could
returnan appropriateCellPos, make the exception bubble up to the next handler (probably the global exception handler, which just prints the exception and stops the program) by passing it tothrowIO, or escape from the program in some other way, likeSystem.Exit.exitFailurein this example.