00001 #ifndef DV_UTIL_SYSTEMERROR_H 00002 #define DV_UTIL_SYSTEMERROR_H 00003 // $Id: systemerror.h,v 1.3 2008/03/12 17:45:16 dvermeir Exp $ 00004 00005 #include <string> 00006 #include <stdexcept> // runtime_error 00007 00008 namespace Dv { 00009 namespace Util { 00010 /** 00011 * Exception class associated with a system error. 00012 * 00013 * Derived from runtime_error. 00014 * 00015 * Example: 00016 * @code 00017 * if (fork() == -1) 00018 * throw SystemError(); 00019 * if (execl(..) == -1) 00020 * throw SystemError("execl failed"); 00021 * if (kill(..,..) == -1) { 00022 * int e = errno; 00023 * throw SystemError(e); 00024 * } 00025 * @endcode 00026 */ 00027 class SystemError: public std::runtime_error { 00028 public: 00029 /** 00030 * Construct new runtime_error exception. 00031 * <code>this->what()</code> will contain NAME + error_code + 00032 * system error message obtained using 00033 * <code>strerror(error_code)</code> 00034 * @param error_code system error code, should be valid 00035 * input for strerror. If 0, the global :errno will 00036 * be used instead. 00037 */ 00038 SystemError(int error_code = 0); 00039 00040 /** 00041 * Construct new runtime_error exception. 00042 * <code>this->what()</code> will contain message + error_code + 00043 * system error message obtained using 00044 * <code>strerror(error_code)</code> 00045 * @param message will be used instead of NAME (see above) 00046 * @param error_code system error code, should be valid 00047 * input for strerror. If 0, the global :errno will 00048 * be used instead. 00049 */ 00050 SystemError(const std::string& message, int error_code = 0); 00051 00052 /** @return error code of this SystemError 00053 * @warning This function is not called errno() because 00054 * <string.h> defines errno as a macro. 00055 */ 00056 int err_no() const { return errno_; } 00057 static std::string strerror(int err=0); 00058 private: 00059 int errno_; 00060 }; 00061 } 00062 } 00063 00064 #endif
dvutil-1.0.10 | [ 5 December, 2009] |