[m-users.] [m-dev.] Pure Functional throwing exception

Charles Shuller charles.shuller at gmail.com
Wed May 8 23:45:50 AEST 2019


Hi Julien,

In the below message you mentioned it would be better NOT to do:

exception.throw("Badness happened, terminating")

Which is something I do quite a lot of to get a non-local exit.   As in,
the exception should never be handled, I just want the program to halt with
some appropriate error message.

It sounded like you recommended the following instead (note the addition of
the exception.software_error):

exception.throw(  exception.software_error("Badness happened,
terminating")  )

Which is a bit more typing, but I don't see how this makes things any
better.   A new type for each kind of exception makes sense to me if I
wanted to handle my exceptions, but boxing all messages as software_error
types doesn't intuitively make sense to me.

Can you explain your reasoning for me??


Thanks!


Charles



On Tue, May 7, 2019 at 12:29 AM Julien Fischer <jfischer at opturion.com>
wrote:

>
> Hi,
>
> On Tue, 7 May 2019, Barthwal wrote:
>
> > Can a pure function throw exceptions?
>
> In Mercury, yes.
>
> > I don't plan to catch these functions in pure function.
>
> If you wanted to, you could.  (Although doing so may affect
> the determinism for your function.)
>
> > But if something illegal happens, I want to send a message like:
> >
> > throw error("Some Message")
>
> Assuming you don't catch the exception within your program, the
> runtime's uncaught exception handler will print the exception out when
> the exception propagates back past main/2.   (It may also print a stack
> trace if you're using a grade that records that information.)
>
> As a general rule, I would recommend not throwing strings about.  The
> library has a builtin type named software_error/1 which is suitable for
> exceptions where something illegal happens.
>
> Julien.
> _______________________________________________
> developers mailing list
> developers at lists.mercurylang.org
> https://lists.mercurylang.org/listinfo/developers
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20190508/f1074afe/attachment.html>


More information about the users mailing list