qa_exception.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <core/exception.h>
00027
00028 #include <stdlib.h>
00029 #include <iostream>
00030 #include <cstdarg>
00031
00032 using namespace fawkes;
00033
00034 class ExampleSmallException : public Exception
00035 {
00036 public:
00037 ExampleSmallException() : Exception("Small Exception") {}
00038 };
00039
00040 class ExampleBigException : public Exception
00041 {
00042 public:
00043 ExampleBigException() : Exception("Big Exception") {}
00044 };
00045
00046 class ExampleUnhandledException : public Exception
00047 {
00048 public:
00049 ExampleUnhandledException() : Exception("Exception not handled") {}
00050 };
00051
00052
00053 void
00054 throw_some_exception()
00055 {
00056 int r = rand();
00057 if ( r < (RAND_MAX / 2)) {
00058 throw ExampleSmallException();
00059 } else if ( r > (RAND_MAX - RAND_MAX / 20)) {
00060
00061
00062 } else {
00063 throw ExampleBigException();
00064 }
00065 }
00066
00067 void
00068 indirect_throw_some_exception()
00069 {
00070 try {
00071 throw_some_exception();
00072 } catch (Exception &e) {
00073 e.append("More info");
00074 throw;
00075 }
00076 }
00077
00078 void
00079 variadic_func(const char *format, ...)
00080 {
00081 va_list va;
00082 va_start(va, format);
00083 throw Exception(format, va);
00084 va_end(va);
00085
00086
00087
00088 }
00089
00090 int
00091 main(int argc, char **argv)
00092 {
00093 srand(42);
00094
00095
00096
00097
00098
00099
00100
00101 while (1) {
00102 try {
00103 indirect_throw_some_exception();
00104 } catch (ExampleSmallException &se) {
00105 std::cout << "Message: " << se.what() << std::endl;
00106 std::cout << "Trace:" << std::endl;
00107 se.print_trace();
00108 } catch (ExampleBigException &be) {
00109 std::cout << "Message: " << be.what() << std::endl;
00110 std::cout << "Trace:" << std::endl;
00111 be.print_trace();
00112 }
00113 }
00114 }
00115
00116
00117