How do I create dynamic exceptions

Bayley, Alistair Alistair_Bayley@ldn.invesco.com
Wed, 23 Jul 2003 08:37:48 +0100


> From: Ross Paterson [mailto:ross@soi.city.ac.uk]
> 
> On Tue, Jul 22, 2003 at 02:59:12PM +0100, Bayley, Alistair wrote:
> > > handler :: Dynamic -> IO ()
> > > handler e = do
> > >   case (fromDynamic e) of
> > >    Nothing -> putStrLn ("dynamic cast failed")
> > >    Just (MkExc n s) -> putStrLn ("exception: " ++ (show 
> n) ++ " " ++ s)
> > 
> > > main :: IO ()
> > > main = catchDyn temp handler
> 
> You're catching a Dynamic wrapped as a Dynamic (catchDyn already does
> fromDynamic, as indicated by its type).  Change the handler to
> 
> > handler :: MyException -> IO ()
> > handler (MkExc n s) = putStrLn ("exception: " ++ (show n) 
> ++ " " ++ s)
> 
> any other dynamic exception fails through to the top level.


Thanks. I was applying some advice from Hal Daume:


> so, what you want is probably something like:
>
> > if rc < 0
> >   then throwIO (DynException (toDyn ("Err", rc)))
> >   else ...
>
> and then when you catch, use fromDyn(amic) to get the value out if you can
> predict its type...


... but in this case what I wanted was (just one more layer of indirection):


> handler :: Exception -> IO ()
> handler e = do
>   case e of
>     DynException err -> case (fromDynamic err) of
>       Nothing -> putStrLn "dynamic cast failure"
>       Just (MkExc n s) -> putStrLn ("exception: " ++ (show n) ++ " " ++ s)
>     _ -> putStrLn "unhandled exception"


> main :: IO ()
> main = catch temp handler  -- use catch rather than catchDyn


*****************************************************************
The information in this email and in any attachments is 
confidential and intended solely for the attention and use 
of the named addressee(s). This information may be 
subject to legal professional or other privilege or may 
otherwise be protected by work product immunity or other 
legal rules.  It must not be disclosed to any person without 
our authority.

If you are not the intended recipient, or a person 
responsible for delivering it to the intended recipient, you 
are not authorised to and must not disclose, copy, 
distribute, or retain this message or any part of it.
*****************************************************************