Fawkes API Fawkes Development Version
qa_exception.cpp
1
2/***************************************************************************
3 * example_exception.cpp - Example for using exceptions
4 *
5 * Generated: Sun Sep 17 14:00:26 2006 (German Medical Library)
6 * Copyright 2006 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
19 *
20 * Read the full text in the LICENSE.GPL file in the doc directory.
21 */
22
23// Do not mention in API doc
24/// @cond EXAMPLES
25
26#include <core/exception.h>
27
28#include <cstdarg>
29#include <iostream>
30#include <stdlib.h>
31
32using namespace fawkes;
33
34class ExampleSmallException : public Exception
35{
36public:
37 ExampleSmallException() : Exception("Small Exception")
38 {
39 }
40};
41
42class ExampleBigException : public Exception
43{
44public:
45 ExampleBigException() : Exception("Big Exception")
46 {
47 }
48};
49
50class ExampleUnhandledException : public Exception
51{
52public:
53 ExampleUnhandledException() : Exception("Exception not handled")
54 {
55 }
56};
57
58void
59throw_some_exception()
60{
61 int r = rand();
62 if (r < (RAND_MAX / 2)) {
63 throw ExampleSmallException();
64 } else if (r > (RAND_MAX - RAND_MAX / 20)) {
65 //printf("Throwing boom\n");
66 //throw ExampleUnhandledException();
67 } else {
68 throw ExampleBigException();
69 }
70}
71
72void
73indirect_throw_some_exception()
74{
75 try {
76 throw_some_exception();
77 } catch (Exception &e) {
78 e.append("More info");
79 throw;
80 }
81}
82
83void
84variadic_func(const char *format, ...)
85{
86 va_list va;
87 va_start(va, format);
88 throw Exception(format, va);
89 va_end(va);
90 /*
91 throw Exception("Format received: %s", format);
92 */
93}
94
95int
96main(int argc, char **argv)
97{
98 srand(42);
99
100 // errno exception
101 // throw Exception(1, "test %i %s", 3, "blub");
102
103 // throw variadic exception
104 // variadic_func("test %i %s %i %f", 4, "haha", 4, 3.2);
105
106 while (1) {
107 try {
108 indirect_throw_some_exception();
109 } catch (ExampleSmallException &se) {
110 std::cout << "Message: " << se.what() << std::endl;
111 std::cout << "Trace:" << std::endl;
112 se.print_trace();
113 } catch (ExampleBigException &be) {
114 std::cout << "Message: " << be.what() << std::endl;
115 std::cout << "Trace:" << std::endl;
116 be.print_trace();
117 }
118 }
119}
120
121/// @endcond
Base class for exceptions in Fawkes.
Definition: exception.h:36
void append(const char *format,...) noexcept
Append messages to the message list.
Definition: exception.cpp:333
Fawkes library namespace.