[m-users.] Converting an exception to a different exception
Julien Fischer
jfischer at opturion.com
Thu May 20 21:14:08 AEST 2021
On Thu, 20 May 2021, Volker Wysk wrote:
> I want do convert an exception from one type to another. Like this:
>
>
> :- pred dir_to_name(pages::in, string::in, string::out) is det.
>
> dir_to_name({ _, DirNameM }, Dir, Name) :-
> ( try []
> map.lookup(DirNameM, Dir, Name)
> then
> true
> catch software_error(_) ->
> throw("Unknown page directory " ++ quote(Dir))
> ).
>
> This won't compile, because "A try goal has determinism cc_multi." (Mercury
> Reference Manual, chapter 12).
>
> Is there any way to do this?
In general, committed choice nondeterminism propagates up the call tree
until either:
1. it reaches main/2 (which will itself then have to be declared
cc_multi to be determinism correct).
2. the programmer explicitly promises it away, e.g. using a
promise_equvalent_solutions scope. (And "promise" means exactly
that, it's not something the compiler can check, it's relying on
you to get it correct.)
For (1) with the above predicate we just need to changes its declared
determinism to cc_multi; for (2) insert the scope.
(The above example compiles if you do either of those things, although
it probably shouldn't since Name is being bound by the try goal even
though it is non-local to the try goal; that looks like a compiler bug.)
Julien.
More information about the users
mailing list