:- module throw_from_foreign.
:- interface.

:- import_module io.

:- pred main(io::di, io::uo) is cc_multi.

:- implementation.

:- import_module exception.

main(!IO) :-
    try_io(chucker("catch me"), _TryResult, !IO),
    write_string(stderr_stream, "end of main\n", !IO).

:- pred chucker(string::in, string::out, io::di, io::uo) is det.

:- pragma foreign_proc("C",
    chucker(String0::in, String::out, _IO0::di, _IO::uo),
    [may_call_mercury, promise_pure, thread_safe],
"
    String = String0;
    dothrow(String);
    fprintf(stderr, ""!!! should be unreachable\\n"");
").

chucker(String, String, !IO) :-
    dothrow(String).

:- pred dothrow(string::in) is erroneous.
:- pragma foreign_export("C", dothrow(in), "dothrow").

dothrow(Message) :-
    throw(Message).
