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
@@ -260,9 +260,9 @@ Connection::Connection( const char *desired_ip, const char *desired_port ) /* se
|
||||
try {
|
||||
if ( try_bind( desired_ip, desired_port_low, desired_port_high ) ) { return; }
|
||||
} catch ( const NetworkException& e ) {
|
||||
fprintf( stderr, "Error binding to IP %s: %s: %s\n",
|
||||
fprintf( stderr, "Error binding to IP %s: %s\n",
|
||||
desired_ip,
|
||||
e.function.c_str(), strerror( e.the_errno ) );
|
||||
e.what() );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -270,8 +270,8 @@ Connection::Connection( const char *desired_ip, const char *desired_port ) /* se
|
||||
try {
|
||||
if ( try_bind( NULL, desired_port_low, desired_port_high ) ) { return; }
|
||||
} catch ( const NetworkException& e ) {
|
||||
fprintf( stderr, "Error binding to any interface: %s: %s\n",
|
||||
e.function.c_str(), strerror( e.the_errno ) );
|
||||
fprintf( stderr, "Error binding to any interface: %s\n",
|
||||
e.what() );
|
||||
throw; /* this time it's fatal */
|
||||
}
|
||||
|
||||
|
||||
+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