https://github.com/aiya000/hs-throwable-exceptions
Give the exception's value constructors for your haskell project
https://github.com/aiya000/hs-throwable-exceptions
exceptions haskell
Last synced: 9 months ago
JSON representation
Give the exception's value constructors for your haskell project
- Host: GitHub
- URL: https://github.com/aiya000/hs-throwable-exceptions
- Owner: aiya000
- License: mit
- Created: 2017-06-12T01:01:16.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-06-29T09:18:49.000Z (over 4 years ago)
- Last Synced: 2025-05-09T00:46:27.578Z (9 months ago)
- Topics: exceptions, haskell
- Language: Haskell
- Size: 31.3 KB
- Stars: 6
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# :diamonds: throwable-exceptions :diamonds:
[](https://travis-ci.org/aiya000/hs-throwable-exceptions)
[](https://hackage.haskell.org/package/throwable-exceptions)
`throwable-exceptions` gives an easy way to create the data types of `Exception` instance with [TemplateHaskell](https://wiki.haskell.org/Template_Haskell),
and gives simple data types of `Exception` instance with its value constructor,
for your haskell project :dog:
- `throwable-exceptions` is available in
- [Hackage](https://hackage.haskell.org/package/throwable-exceptions)
- [stackage (nightly build)](https://www.stackage.org/nightly-2017-06-18/package/throwable-exceptions)
## :books: Document is available in here :books:
- [throwable-exceptions - Hackage](https://hackage.haskell.org/package/throwable-exceptions)
# :muscle: Why should we use this ? :muscle:
We want to throw some exception frequently, but the mostly throwable exceptions are not given by `base`.
`throwable-exceptions` complements it :+1:
## Examples
- vvv The summary of the exact examples is available here vvv
- [test/MainTest.hs](https://github.com/aiya000/hs-throwable-exceptions/blob/master/test/MainTest.hs)
- - -
You can create a data type of `Exception` instance by **a line** :exclamation:
```haskell
{-# LANGUAGE TemplateHaskell #-}
module Main where
-- This is same as
--
-- data MyException a = MyException
-- { myExceptionCause :: String
-- , myExceptionClue :: a
-- } deriving (Show, Typeable)
-- instance (Typeable a, Show a) => Exception (MyException a)
--
declareException "MyException" ["MyException"]
-- This is same as
--
-- data TwoException a =
-- FirstException
-- { firstExceptionCause :: String
-- , firstExceptionClue :: a
-- } |
-- SecondException
-- { secondExceptionCause :: String
-- , secondExceptionClue :: a
-- } deriving (Typeable)
--
-- instance Show a => (TwoException a) where
-- ...
-- instance (Typeable a, Show a) => Exception (TwoException a)
--
declareException "TwoException" ["FirstException", "SecondException"]
main :: IO ()
main = do
print $ ([1..4] `at` 5 :: Either SomeException Int)
print $ MyException "hi" 10
print $ myException "poi"
print $ firstException "chino"
print $ secondException "cocoa"
```
- the completely example
- in [example/Main.hs](https://github.com/aiya000/hs-throwable-exceptions/blob/master/example/Main.hs)
- - -
Several exceptions are defined by default :smile:
For example, [IOException](https://hackage.haskell.org/package/base-4.9.1.0/docs/Control-Exception.html#t:IOException)'s value constructor is not given :cry:
But you can use `Control.Exception.Throwable.IOException'` instead :dog:
```haskell
module Main where
main :: IO ()
main = do
throwM $ IOException' "oops!" "in main"
throwM $ ioException' "oops!"
```
# :+1:
PR is welcome !