Exception Handling

examples/api/cpp/exceptions.cpp

 1/******************************************************************************
 2 * Top contributors (to current version):
 3 *   Daniel Larraz
 4 *
 5 * This file is part of the cvc5 project.
 6 *
 7 * Copyright (c) 2009-2025 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 C++ API.
14 *
15 * A simple demonstration of catching cvc5 exceptions via the C++ API.
16 */
17
18#include <cvc5/cvc5.h>
19#include <iostream>
20
21int main()
22{
23  cvc5::TermManager tm;
24  cvc5::Solver solver(tm);
25
26  solver.setOption("produce-models", "true");
27
28  // Setting an invalid option
29  try
30  {
31    solver.setOption("non-existing", "true");
32    return 1;
33  }
34  catch (const std::exception& e)
35  {
36    std::cout << e.what() << std::endl;
37  }
38
39  // Creating a term with an invalid type
40  try
41  {
42    cvc5::Sort integer = tm.getIntegerSort();
43    cvc5::Term x = tm.mkVar(integer, "x");
44    cvc5::Term invalidTerm = tm.mkTerm(cvc5::Kind::AND, {x, x});
45    solver.checkSatAssuming(invalidTerm);
46    return 1;
47  }
48  catch (const std::exception& e)
49  {
50    std::cout << e.what() << std::endl;
51  }
52
53  // Asking for a model after unsat result
54  try
55  {
56    solver.checkSatAssuming(tm.mkBoolean(false));
57    solver.getModel({}, {});
58    return 1;
59  }
60  catch (const std::exception& e)
61  {
62    std::cout << e.what() << std::endl;
63  }
64
65  return 0;
66}