[m-users.] Java exceptions and nested classes
Mark Clements
mark.clements at ki.se
Fri Sep 19 20:31:22 AEST 2025
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>
From the generated code, I see:
public static class Result_2<MR_tvar_1, MR_tvar_2>
implements jmercury.runtime.MercuryType
{
public int data_tag;
public static class Error_1<MR_tvar_1, MR_tvar_2>
extends mve.Result_2
{
<snipped>
Should the definition for Error_1 be:
public static class Error_1<MR_tvar_1, MR_tvar_2>
extends mve.Result_2<MR_tvar_1, MR_tvar_2>
{
<snipped>
I note that the mlds_class_inherits type does not include type_param, so that output_generic_tvars() cannot be called for the inherited class.
Alternatively, is there a better way to catch the Java exceptions?
--- 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>.
More information about the users
mailing list