Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7065165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:55:23+00:00 2026-05-28T04:55:23+00:00

Trying to throw an exception in Haskell: import Control.Exception import Data.Typeable data MyException =

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:55:24+00:00Added an answer on May 28, 2026 at 4:55 am

    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 DeriveDataTypeable is actually the recommended way to do this. To enable the extension, you can put:

    {-# LANGUAGE DeriveDataTypeable #-}
    

    at the top of your source file. Passing -XDeriveDataTypeable on 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 throw with throwIO in the definition of test, as it guarantees the correct ordering in the IO monad.

    You should also add

    import Prelude hiding (catch)
    

    above your imports, as the Prelude’s catch is 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:

    example :: IO ()
    example = do
      result <- test "hello" `catch` handler
      ...
      where handler ThisException = putStrLn "Oh no!" >> exitFailure
            handler ThatException = putStrLn "Yikes!" >> exitFailure
    

    (The foo `catch` bar syntax is the same as catch 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 return an appropriate CellPos, 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 to throwIO, or escape from the program in some other way, like System.Exit.exitFailure in this example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to throw an exception in my code like this: throw RuntimeException(msg); But
I am using rhino mocks 3.5 and am trying to throw an exception in
I am trying to throw an exception based on the exception type parameter passed
I'm trying to throw a custom exception. The implementation of the custom exception class
this code throw exception while i am trying to update value ,first value only
I have some code where i am trying to throw a custom exception in
I am trying unsuccessfully to throw an exception TooLongEx if a user input fails.
In trying to refactor some I code I attempted to throw the exception in
Regarding the following code - I'm trying to get it throw and exception if
I'm trying to throw new Exception(...) in a script written for PHP CLI. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.