1 // Exception 2 3 #ifndef EXCEPTION_H 4 #define EXCEPTION_H 5 6 #include <stdarg.h> 7 #include <stdio.h> 8 9 #include <String.h> 10 11 class Exception { 12 public: 13 // constructor 14 Exception() 15 : fError(B_OK), 16 fDescription() 17 { 18 } 19 20 // constructor 21 Exception(BString description) 22 : fError(B_OK), 23 fDescription(description) 24 { 25 } 26 27 // constructor 28 Exception(const char* format,...) 29 : fError(B_OK), 30 fDescription() 31 { 32 va_list args; 33 va_start(args, format); 34 SetTo(B_OK, format, args); 35 va_end(args); 36 } 37 38 // constructor 39 Exception(status_t error) 40 : fError(error), 41 fDescription() 42 { 43 } 44 45 // constructor 46 Exception(status_t error, BString description) 47 : fError(error), 48 fDescription(description) 49 { 50 } 51 52 // constructor 53 Exception(status_t error, const char* format,...) 54 : fError(error), 55 fDescription() 56 { 57 va_list args; 58 va_start(args, format); 59 SetTo(error, format, args); 60 va_end(args); 61 } 62 63 // copy constructor 64 Exception(const Exception& exception) 65 : fError(exception.fError), 66 fDescription(exception.fDescription) 67 { 68 } 69 70 // destructor 71 ~Exception() 72 { 73 } 74 75 // SetTo 76 void SetTo(status_t error, BString description) 77 { 78 fError = error; 79 fDescription.SetTo(description); 80 } 81 82 // SetTo 83 void SetTo(status_t error, const char* format, va_list arg) 84 { 85 char buffer[2048]; 86 vsprintf(buffer, format, arg); 87 SetTo(error, BString(buffer)); 88 } 89 90 // GetError 91 status_t GetError() const 92 { 93 return fError; 94 } 95 96 // GetDescription 97 const char* GetDescription() const 98 { 99 return fDescription.String(); 100 } 101 102 private: 103 status_t fError; 104 BString fDescription; 105 }; 106 107 #endif // EXCEPTION_H 108