[m-users.] Java exceptions and nested classes

Mark Clements mark.clements at ki.se
Mon Sep 22 14:26:52 AEST 2025


Julien: thank you for this. Your mercury-java library has been a key resource.

I have submitted a pull request (https://github.com/Mercury-Language/mercury/pull/139) that adds generic types to the "extends" clause, which addresses mve.m. This means that we can write Java code to catch exceptions and write them to Mercury types (from Java).

By my understanding, we would need to add generic types for "new" Mercury types which have generic types (e.g. as per mve2.m and mve3.m). This is not addressed by the submitted pull request.

---- Mark

________________________________
From: Julien Fischer <jfischer at opturion.com>
Sent: Friday, September 19, 2025 3:27:19 PM
To: Mark Clements <mark.clements at ki.se>
Cc: users at lists.mercurylang.org <users at lists.mercurylang.org>
Subject: Re: [m-users.] Java exceptions and nested classes

[You don't often get email from jfischer at opturion.com. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

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://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fjuliensf%2Fmercury-java&data=05%7C02%7Cmark.clements%40ki.se%7C5ded018e0c9f4f336f8108ddf7804a55%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C0%7C0%7C638938853514514300%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C60000%7C%7C%7C&sdata=67vstV8DKDJ%2BjI3onCJyWrbb3EoVUTvp2XgbhWtOekI%3D&reserved=0<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://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fki.se%2Fom-ki%2Fintegritetsskyddspolicy&data=05%7C02%7Cmark.clements%40ki.se%7C5ded018e0c9f4f336f8108ddf7804a55%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C0%7C0%7C638938853514544291%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C60000%7C%7C%7C&sdata=lntdNO1jOwSqG1ZPp8lNisKASWQ%2BsNOA5UPfHJ9w8oA%3D&reserved=0<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://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.mercurylang.org%2Flistinfo%2Fusers&data=05%7C02%7Cmark.clements%40ki.se%7C5ded018e0c9f4f336f8108ddf7804a55%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C0%7C0%7C638938853514565787%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C60000%7C%7C%7C&sdata=lSTPQxf8lrNqHNC%2B%2BeSgu8o96ZR81THpbOogzCv3BCM%3D&reserved=0<https://lists.mercurylang.org/listinfo/users>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20250922/bae727f3/attachment.html>


More information about the users mailing list