Exception Handling

examples/api/java/Exceptions.java

 1/******************************************************************************
 2 * Top contributors (to current version):
 3 *   Mudathir Mohamed, Andres Noetzli
 4 *
 5 * This file is part of the cvc5 project.
 6 *
 7 * Copyright (c) 2009-2024 by the authors listed in the file AUTHORS
 8 * in the top-level source directory and their institutional affiliations.
 9 * All rights reserved.  See the file COPYING in the top-level source
10 * directory for licensing information.
11 * ****************************************************************************
12 *
13 * Catching cvc5 exceptions via the Java API.
14 *
15 * A simple demonstration of catching cvc5 execptions via the Java API.
16 */
17
18import io.github.cvc5.*;
19
20public class Exceptions
21{
22  public static void main(String[] args)
23  {
24    TermManager tm = new TermManager();
25    Solver solver = new Solver(tm);
26    {
27      solver.setOption("produce-models", "true");
28
29      // Setting an invalid option
30      try
31      {
32        solver.setOption("non-existing", "true");
33        System.exit(1);
34      }
35      catch (Exception e)
36      {
37        System.out.println(e.toString());
38      }
39
40      // Creating a term with an invalid type
41      try
42      {
43        Sort integer = tm.getIntegerSort();
44        Term x = tm.mkVar(integer, "x");
45        Term invalidTerm = tm.mkTerm(Kind.AND, x, x);
46        solver.checkSatAssuming(invalidTerm);
47        System.exit(1);
48      }
49      catch (Exception e)
50      {
51        System.out.println(e.toString());
52      }
53
54      // Asking for a model after unsat result
55      try
56      {
57        solver.checkSatAssuming(tm.mkBoolean(false));
58        solver.getModel(new Sort[] {}, new Term[] {});
59        System.exit(1);
60      }
61      catch (Exception e)
62      {
63        System.out.println(e.toString());
64      }
65    }
66    Context.deletePointers();
67  }
68}