[m-users.] Java exceptions and nested classes

Julien Fischer jfischer at opturion.com
Fri Sep 19 23:27:19 AEST 2025


Hi Mark,

On Fri, 19 Sept 2025 at 21:05, Mark Clements <mark.clements at ki.se> wrote:
>
> I am investigating using JDBC with Mercury. Most of the JDBC classes throw an SQLException, which I want to catch in Java and return a result type with either an ok() value or an error(). I have the following minimal example (not using JDBC):
>
> :- module mve.
> :- interface.
> :- import_module io, int.
> :- type result(T,E) ---> ok(T) ; error(E).
> :- type exception.
> :- pred some_pred(int::in, mve.result(int,exception)::out, io::di, io::uo) is det.
> :- pred main(io::di, io::uo) is det.
> :- implementation.
> :- pragma foreign_type("Java", exception, "java.lang.Exception").
> :- pragma foreign_proc("Java",
>     some_pred(A::in, C::out, _IO0::di, _IO1::uo),
>     [will_not_call_mercury, thread_safe, promise_pure],
>     "try {
>          C = new Result_2.Ok_1<Integer,java.lang.Exception>(A);
>     } catch(java.lang.Exception e) {
>          System.err.println(e.getMessage());
>          C = new Result_2.Error_1<Integer,java.lang.Exception>(e);
>     }").
> main(!IO) :-
>     some_pred(1, C, !IO),
>     (C = ok(_) ; C = error(_)).
>
> However, when I compile this I get warnings, such as:
>
> mve.m:15: warning: [unchecked] unchecked conversion
>          C = new Result_2.Ok_1<Integer,java.lang.Exception>(A);
>              ^
>   required: Result_2<Integer,Exception>
>   found:    Ok_1<Integer,Exception>

Under the current compilation scheme for Java, you are not going to be able
to avoid those warnings.  Mercury's use of generics in the Java grades is
rather limited.  (Much of the generated code effectively works with raw types.)

> Alternatively, is there a better way to catch the Java exceptions?

I assume you mean: is there a better way to pass them back to Mercury?
(Obviously, the only way to catch them is to use the exception
handling mechanisms
that Java provides.)

I would avoid relying on how Mercury represents types in Java and either use
exported functions to build the result terms (as per the attached
mve2.m example)
or just return a flag from the Java code indicating what has happened and deal
with constructing result terms in Mercury (as per the attached mve3.m example).

(Some time ago I wrote <https://github.com/juliensf/mercury-java>, which not
covering JDBC, may give you some idea for how to approach interfacing with
Java APIs.)

Julien.








>
> --- Mark.
>
>
> När du skickar e-post till Karolinska Institutet (KI) innebär detta att KI kommer att behandla dina personuppgifter. Här finns information om hur KI behandlar personuppgifter<https://ki.se/om-ki/integritetsskyddspolicy>.
>
>
> Sending email to Karolinska Institutet (KI) will result in KI processing your personal data. You can read more about KI’s processing of personal data here<https://staff.ki.se/data-protection-policy>.
> _______________________________________________
> users mailing list
> users at lists.mercurylang.org
> https://lists.mercurylang.org/listinfo/users
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mve2.m
Type: application/octet-stream
Size: 1038 bytes
Desc: not available
URL: <http://lists.mercurylang.org/archives/users/attachments/20250919/eabb140a/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mve3.m
Type: application/octet-stream
Size: 1056 bytes
Desc: not available
URL: <http://lists.mercurylang.org/archives/users/attachments/20250919/eabb140a/attachment-0001.obj>


More information about the users mailing list