Exception Handling

examples/api/cpp/exceptions.cpp

 1/******************************************************************************
 2 * This file is part of the cvc5 project.
 3 *
 4 * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
 5 * in the top-level source directory and their institutional affiliations.
 6 * All rights reserved.  See the file COPYING in the top-level source
 7 * directory for licensing information.
 8 * ****************************************************************************
 9 *
10 * Catching cvc5 exceptions via the C++ API.
11 *
12 * A simple demonstration of catching cvc5 exceptions via the C++ API.
13 */
14
15#include <cvc5/cvc5.h>
16
17#include <iostream>
18
19int main()
20{
21  cvc5::TermManager tm;
22  cvc5::Solver solver(tm);
23
24  solver.setOption("produce-models", "true");
25
26  // Setting an invalid option
27  try
28  {
29    solver.setOption("non-existing", "true");
30    return 1;
31  }
32  catch (const std::exception& e)
33  {
34    std::cout << e.what() << std::endl;
35  }
36
37  // Creating a term with an invalid type
38  try
39  {
40    cvc5::Sort integer = tm.getIntegerSort();
41    cvc5::Term x = tm.mkVar(integer, "x");
42    cvc5::Term invalidTerm = tm.mkTerm(cvc5::Kind::AND, {x, x});
43    solver.checkSatAssuming(invalidTerm);
44    return 1;
45  }
46  catch (const std::exception& e)
47  {
48    std::cout << e.what() << std::endl;
49  }
50
51  // Asking for a model after unsat result
52  try
53  {
54    solver.checkSatAssuming(tm.mkBoolean(false));
55    solver.getModel({}, {});
56    return 1;
57  }
58  catch (const std::exception& e)
59  {
60    std::cout << e.what() << std::endl;
61  }
62
63  return 0;
64}