This is the old c++ interface:
matthias@DESKTOP-A2T8IFC:~/exception$ cat likes.cpp
#include "SWI-cpp.h"
#include <iostream>
int main(int argc, char** argv)
{
PlEngine e(argc, argv) ;
PlTermv noargs(0) ;
PlQuery q("license1", noargs) ;
try
{ q.next_solution() ;
}
catch(PlException& ex)
{ std::cerr << (char*) ex << std::endl ;
}
return 0 ;
}
Running it:
matthias@DESKTOP-A2T8IFC:~/exception$ swipl-ld likes.cpp
matthias@DESKTOP-A2T8IFC:~/exception$ ./a.out
Welcome to SWI-Prolog (threaded, 64 bits, version 9.1.2-4-g65fab31ed)
(etc.)
For built-in help, use ?- help(Topic). or ?- apropos(Word).
error(existence_error(procedure,license1/0),context(system:'$c_call_prolog'/0,_6776))
This is the new one:
matthias@DESKTOP-A2T8IFC:~/exception$ cat likes2.cpp
#include "SWI-cpp2.h"
#include <iostream>
int main(int argc, char** argv)
{
PlEngine e(argc, argv) ;
PlTermv noargs(0) ;
PlQuery q("license1", noargs) ;
try
{ if(!q.next_solution())
std::cerr << "query failed" << std::endl ;
}
catch(PlException& ex)
{ std::cerr << "caught exception" << std::endl ;
std::cerr << ex.as_string() << std::endl ;
}
return 0 ;
}
Compilation and run:
matthias@DESKTOP-A2T8IFC:~/exception$ swipl-ld likes2.cpp
matthias@DESKTOP-A2T8IFC:~/exception$ ./a.out
Welcome to SWI-Prolog (threaded, 64 bits, version 9.1.2-4-g65fab31ed)
(etc.)
For built-in help, use ?- help(Topic). or ?- apropos(Word).
query failed
matthias@DESKTOP-A2T8IFC:~/exception$
What do I need to do to catch the exception? I played around with the flags (optional 3rd argument to PlQuery), it didn’t really help.