Make all exception classes inherit from std::exception
This refactors out a very common pattern of formatting "%s: %s" with e.function.c_str() and strerror( e.the_errno ) into just the what() method of NetworkException. It's also a prerequisite for making cleaner public API for any exceptions we throw, and allows us to more easily get exceptions passed back to us to handle.
This commit is contained in:
committed by
John Hood
parent
ebecb9bd3a
commit
5721b392ab
+11
-3
@@ -41,6 +41,8 @@
|
||||
#include <math.h>
|
||||
#include <vector>
|
||||
#include <assert.h>
|
||||
#include <exception>
|
||||
#include <string.h>
|
||||
|
||||
#include "crypto.h"
|
||||
|
||||
@@ -53,12 +55,18 @@ namespace Network {
|
||||
uint16_t timestamp16( void );
|
||||
uint16_t timestamp_diff( uint16_t tsnew, uint16_t tsold );
|
||||
|
||||
class NetworkException {
|
||||
class NetworkException : public std::exception {
|
||||
public:
|
||||
string function;
|
||||
int the_errno;
|
||||
NetworkException( string s_function, int s_errno ) : function( s_function ), the_errno( s_errno ) {}
|
||||
NetworkException() : function( "<none>" ), the_errno( 0 ) {}
|
||||
private:
|
||||
string my_what;
|
||||
public:
|
||||
NetworkException( string s_function="<none>", int s_errno=0)
|
||||
: function( s_function ), the_errno( s_errno ),
|
||||
my_what(function + ": " + strerror(the_errno)) {}
|
||||
const char *what() const throw () { return my_what.c_str(); }
|
||||
~NetworkException() throw () {}
|
||||
};
|
||||
|
||||
enum Direction {
|
||||
|
||||
Reference in New Issue
Block a user