float media( vector ingresos ) throw( string ){ if ( ingresos.size()==0 ) { throw (string)"División por cero en función media"; } float toRet; for( int i=0; i<ingresos.size(); ++i ) { toRet+=ingresos[i]; } return toRet/ingresos.size(); }
float mediaNotas( vector notas ) throw( std:out_of_range string ){ if ( notas.size()==0 ) { throw (string)"División por cero en función mediaNotas"; } float toRet; for( int i=0; i<ingresos.size(); ++i ) { toRet+=ingresos[i]; } toRet=toRet/ingresos.size(); if( toRet10 ) { throw std::out_of_range("Media de notas en rango incorrecto"); } return toRet; }
void muestraInformacion( vector ingresos ) { try { for( int i=0; i<ingresos.size(); ++i ) { cout << ingresos.at(i)<<endl; } cout << "La media es "<<media(ingresos)<<endl; } catch ( string mje ) { cout << "No se pudo calcular la media: " << mje << endl; } catch ( std::out_of_range& e ) { cout << "Problemas al acceder a " << "los elementos del vector: " << e.what() << endl; } catch ( std::exception& x ) { cout << "Error en muestraInformacion " << x.what() << endl; } catch( ... ) { cout << "No se pudo calcular la media " << " y no tengo ni idea de por qué :) " << endl; } } class ElefanteVolador: public std::exception { private: string mensaje; public: const char* what() const trhow() { return mensaje.c_str(); } }