Fawkes API Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * example_exception.cpp - Example for using exceptions 00004 * 00005 * Generated: Sun Sep 17 14:00:26 2006 (German Medical Library) 00006 * Copyright 2006 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * Read the full text in the LICENSE.GPL file in the doc directory. 00021 */ 00022 00023 // Do not mention in API doc 00024 /// @cond EXAMPLES 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 //printf("Throwing boom\n"); 00061 //throw ExampleUnhandledException(); 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 throw Exception("Format received: %s", format); 00087 */ 00088 } 00089 00090 int 00091 main(int argc, char **argv) 00092 { 00093 srand(42); 00094 00095 // errno exception 00096 // throw Exception(1, "test %i %s", 3, "blub"); 00097 00098 // throw variadic exception 00099 // variadic_func("test %i %s %i %f", 4, "haha", 4, 3.2); 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 /// @endcond