Skip to content

Commit f257667

Browse files
committed
Implementation of a erroneous user input exception class.
1 parent 8427503 commit f257667

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

src/cbmc/cbmc_main.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ int wmain(int argc, const wchar_t **argv_wide)
4141
int main(int argc, const char **argv)
4242
{
4343
#endif
44-
cbmc_parse_optionst parse_options(argc, argv);
44+
try
45+
{
46+
cbmc_parse_optionst parse_options(argc, argv);
4547

46-
int res=parse_options.main();
48+
int res = parse_options.main();
4749

4850
#ifdef IREP_HASH_STATS
4951
std::cout << "IREP_HASH_CNT=" << irep_hash_cnt << '\n';
@@ -52,4 +54,9 @@ int main(int argc, const char **argv)
5254
#endif
5355

5456
return res;
57+
}
58+
catch(invalid_user_input_exceptiont &e)
59+
{
60+
std::cerr << e.what() << std::endl;
61+
}
5562
}

src/util/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ SRC = arith_tools.cpp \
1313
expr.cpp \
1414
expr_initializer.cpp \
1515
expr_util.cpp \
16+
exception_utils.cpp \
1617
file_util.cpp \
1718
find_macros.cpp \
1819
find_symbols.cpp \

src/util/exception_utils.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*******************************************************************\
2+
3+
Module: Exception helper utilities
4+
5+
Author: Fotis Koutoulakis, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#include "exception_utils.h"
10+
11+
std::string invalid_user_input_exceptiont::what() const noexcept
12+
{
13+
std::string res;
14+
res += "\nInvalid User Input\n";
15+
res += "Option: " + option + "\n";
16+
res += "Reason: " + reason;
17+
// Print an optional correct usage message assuming correct input parameters have been passed
18+
res += correct_input.empty() ? "\n" : " Try: " + correct_input + "\n";
19+
return res;
20+
}

src/util/exception_utils.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*******************************************************************\
2+
3+
Module: Exception helper utilities
4+
5+
Author: Fotis Koutoulakis, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_UTIL_EXCEPTION_UTILS_H
10+
#define CPROVER_UTIL_EXCEPTION_UTILS_H
11+
12+
#include <string>
13+
14+
class invalid_user_input_exceptiont
15+
{
16+
std::string reason;
17+
std::string option;
18+
std::string correct_input;
19+
20+
public:
21+
invalid_user_input_exceptiont(
22+
std::string reason,
23+
std::string option,
24+
std::string correct_input = "")
25+
: reason(reason), option(option), correct_input(correct_input)
26+
{
27+
}
28+
29+
std::string what() const noexcept;
30+
};
31+
32+
#endif // CPROVER_UTIL_EXCEPTION_UTILS_H

0 commit comments

Comments
 (0)