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 <stdlib.h>
29 #include <iostream>
30 #include <cstdarg>
31 
32 using namespace fawkes;
33 
34 class ExampleSmallException : public Exception
35 {
36  public:
37  ExampleSmallException() : Exception("Small Exception") {}
38 };
39 
40 class ExampleBigException : public Exception
41 {
42  public:
43  ExampleBigException() : Exception("Big Exception") {}
44 };
45 
46 class ExampleUnhandledException : public Exception
47 {
48  public:
49  ExampleUnhandledException() : Exception("Exception not handled") {}
50 };
51 
52 
53 void
54 throw_some_exception()
55 {
56  int r = rand();
57  if ( r < (RAND_MAX / 2)) {
58  throw ExampleSmallException();
59  } else if ( r > (RAND_MAX - RAND_MAX / 20)) {
60  //printf("Throwing boom\n");
61  //throw ExampleUnhandledException();
62  } else {
63  throw ExampleBigException();
64  }
65 }
66 
67 void
68 indirect_throw_some_exception()
69 {
70  try {
71  throw_some_exception();
72  } catch (Exception &e) {
73  e.append("More info");
74  throw;
75  }
76 }
77 
78 void
79 variadic_func(const char *format, ...)
80 {
81  va_list va;
82  va_start(va, format);
83  throw Exception(format, va);
84  va_end(va);
85  /*
86  throw Exception("Format received: %s", format);
87  */
88 }
89 
90 int
91 main(int argc, char **argv)
92 {
93  srand(42);
94 
95  // errno exception
96  // throw Exception(1, "test %i %s", 3, "blub");
97 
98  // throw variadic exception
99  // variadic_func("test %i %s %i %f", 4, "haha", 4, 3.2);
100 
101  while (1) {
102  try {
103  indirect_throw_some_exception();
104  } catch (ExampleSmallException &se) {
105  std::cout << "Message: " << se.what() << std::endl;
106  std::cout << "Trace:" << std::endl;
107  se.print_trace();
108  } catch (ExampleBigException &be) {
109  std::cout << "Message: " << be.what() << std::endl;
110  std::cout << "Trace:" << std::endl;
111  be.print_trace();
112  }
113  }
114 }
115 
116 
117 /// @endcond
Fawkes library namespace.
Base class for exceptions in Fawkes.
Definition: exception.h:36
void append(const char *format,...)
Append messages to the message list.
Definition: exception.cpp:341