Throw std::exception subclasses instead of std::strings

Now everything we throw or catch ourselves is a subclass of
std::exception.
This commit is contained in:
Geoffrey Thomas
2013-08-03 17:23:06 -07:00
committed by John Hood
parent aeffb71cfc
commit b5ac92491c
2 changed files with 10 additions and 9 deletions
+2 -2
View File
@@ -181,8 +181,8 @@ int main( int argc, char *argv[] )
} catch ( const Crypto::CryptoException &e ) { } catch ( const Crypto::CryptoException &e ) {
fprintf( stderr, "Crypto exception: %s\r\n", fprintf( stderr, "Crypto exception: %s\r\n",
e.what() ); e.what() );
} catch ( const std::string &s ) { } catch ( const std::exception &e ) {
fprintf( stderr, "Error: %s\r\n", s.c_str() ); fprintf( stderr, "Error: %s\r\n", e.what() );
} }
printf( "\n[mosh is exiting.]\n" ); printf( "\n[mosh is exiting.]\n" );
+8 -7
View File
@@ -37,6 +37,7 @@
#include "terminaldisplay.h" #include "terminaldisplay.h"
#include <string> #include <string>
#include <stdexcept>
#if defined HAVE_NCURSESW_CURSES_H #if defined HAVE_NCURSESW_CURSES_H
# include <ncursesw/curses.h> # include <ncursesw/curses.h>
@@ -65,7 +66,7 @@ bool Display::ti_flag( const char *capname ) const
{ {
int val = tigetflag( const_cast<char *>( capname ) ); int val = tigetflag( const_cast<char *>( capname ) );
if ( val == -1 ) { if ( val == -1 ) {
throw std::string( "Invalid terminfo boolean capability " ) + capname; throw std::invalid_argument( std::string( "Invalid terminfo boolean capability " ) + capname );
} }
return val; return val;
} }
@@ -74,7 +75,7 @@ int Display::ti_num( const char *capname ) const
{ {
int val = tigetnum( const_cast<char *>( capname ) ); int val = tigetnum( const_cast<char *>( capname ) );
if ( val == -2 ) { if ( val == -2 ) {
throw std::string( "Invalid terminfo numeric capability " ) + capname; throw std::invalid_argument( std::string( "Invalid terminfo numeric capability " ) + capname );
} }
return val; return val;
} }
@@ -83,7 +84,7 @@ const char *Display::ti_str( const char *capname ) const
{ {
const char *val = tigetstr( const_cast<char *>( capname ) ); const char *val = tigetstr( const_cast<char *>( capname ) );
if ( val == (const char *)-1 ) { if ( val == (const char *)-1 ) {
throw std::string( "Invalid terminfo string capability " ) + capname; throw std::invalid_argument( std::string( "Invalid terminfo string capability " ) + capname );
} }
return val; return val;
} }
@@ -98,16 +99,16 @@ Display::Display( bool use_environment )
if ( ret != OK ) { if ( ret != OK ) {
switch ( errret ) { switch ( errret ) {
case 1: case 1:
throw std::string( "Terminal is hardcopy and cannot be used by curses applications." ); throw std::runtime_error( "Terminal is hardcopy and cannot be used by curses applications." );
break; break;
case 0: case 0:
throw std::string( "Unknown terminal type." ); throw std::runtime_error( "Unknown terminal type." );
break; break;
case -1: case -1:
throw std::string( "Terminfo database could not be found." ); throw std::runtime_error( "Terminfo database could not be found." );
break; break;
default: default:
throw std::string( "Unknown terminfo error." ); throw std::runtime_error( "Unknown terminfo error." );
break; break;
} }
} }