|
Re: Exception handling: point is...?
#include <iostream> #include <stdexcept> #include <boost/format.hpp> // for easy string building int factorial( int n ) { if ( n < 0 ) { using namespace boost; format errmsg( "Can't compute (%d)!" ); throw std::invalid_argument( (errmsg % n).str() ); } int result = 1; while ( n != 0 ) result *= n--; return result; } int main() { try { std::cout << factorial( 3 ) << std::endl; std::cout << factorial( 0 ) << std::endl; std::cout << factorial( -2 ) << std::endl; } catch ( std::invalid_argument &e ) { std::cerr << e.what() << std::endl; } }

__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
|