Re: [m-users.] Java exceptions and nested classes
Julien Fischer <[email protected]> Fri, 19 Sep 2025 23:27:19 +1000
| Newsgroups | gmane.comp.lang.mercury.general |
|---|---|
| Message-ID | <CAFFSLtimE-9uau26Y0yiAs0jvMs6r0jtXfOAS8xXjCdxkMsg8g@mail.gmail.com> |
Hi Mark, On Fri, 19 Sept 2025 at 21:05, Mark Clements <[email protected]> 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 > [email protected] > https://lists.mercurylang.org/listinfo/users _______________________________________________ users mailing list [email protected] https://lists.mercurylang.org/listinfo/users
mve2.m
(application/octet-stream, 1 KB) - not displayed
mve3.m
(application/octet-stream, 1 KB) - not displayed