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#include <iostream>
17
18int main()
19{
20  cvc5::TermManager tm;
21  cvc5::Solver solver(tm);
22
23  solver.setOption("produce-models", "true");
24
25  // Setting an invalid option
26  try
27  {
28    solver.setOption("non-existing", "true");
29    return 1;
30  }
31  catch (const std::exception& e)
32  {
33    std::cout << e.what() << std::endl;
34  }
35
36  // Creating a term with an invalid type
37  try
38  {
39    cvc5::Sort integer = tm.getIntegerSort();
40    cvc5::Term x = tm.mkVar(integer, "x");
41    cvc5::Term invalidTerm = tm.mkTerm(cvc5::Kind::AND, {x, x});
42    solver.checkSatAssuming(invalidTerm);
43    return 1;
44  }
45  catch (const std::exception& e)
46  {
47    std::cout << e.what() << std::endl;
48  }
49
50  // Asking for a model after unsat result
51  try
52  {
53    solver.checkSatAssuming(tm.mkBoolean(false));
54    solver.getModel({}, {});
55    return 1;
56  }
57  catch (const std::exception& e)
58  {
59    std::cout << e.what() << std::endl;
60  }
61
62  return 0;
63}