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 
32 using namespace fawkes;
33 
34 class ExampleSmallException : public Exception
35 {
36 public:
37  ExampleSmallException() : Exception("Small Exception")
38  {
39  }
40 };
41 
42 class ExampleBigException : public Exception
43 {
44 public:
45  ExampleBigException() : Exception("Big Exception")
46  {
47  }
48 };
49 
50 class ExampleUnhandledException : public Exception
51 {
52 public:
53  ExampleUnhandledException() : Exception("Exception not handled")
54  {
55  }
56 };
57 
58 void
59 throw_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 
72 void
73 indirect_throw_some_exception()
74 {
75  try {
76  throw_some_exception();
77  } catch (Exception &e) {
78  e.append("More info");
79  throw;
80  }
81 }
82 
83 void
84 variadic_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 
95 int
96 main(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
fawkes::Exception::append
void append(const char *format,...)
Append messages to the message list.
Definition: exception.cpp:332
fawkes
fawkes::Exception
Definition: exception.h:39