Witam
Co zyskujemy tworząc klasę błędów która dziedziczy z std::exception tak jak w poniższym przykładzie?
//======================================================================
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
//======================================================================
using namespace std;
//======================================================================
class WartoscException: public std::exception{
protected:
unsigned int _NR_LINI;
string _NAZWA_PLIKU;
protected:
string m_ExceptionString;
protected:
WartoscException();
public:
WartoscException(string aStr, char* _plik = NULL, unsigned int _linia = 0 );
virtual string ToString() throw();
virtual ~WartoscException() throw(){}
virtual string GetFile(){ return _NAZWA_PLIKU; }
virtual unsigned int GetLine(){ return _NR_LINI; }
};
//======================================================================
WartoscException::WartoscException(){
this->m_ExceptionString = string("BLAD!");
this->_NR_LINI = (unsigned int)(-1);
this->_NAZWA_PLIKU = "-";
}
//----------------------------------------------------------------------
WartoscException::WartoscException(string aStr, char* _plik, unsigned int _linia ){
this->m_ExceptionString = aStr;
this->_NR_LINI = _linia;
this->_NAZWA_PLIKU = (_plik != NULL)?(_plik):("-");
}
//----------------------------------------------------------------------
string WartoscException::ToString() throw(){
return m_ExceptionString;
};
//======================================================================
class NowaWartoscException: public WartoscException{
protected:
float m_Wartosc;
private:
NowaWartoscException(){}
public:
NowaWartoscException(string aStr, char* _plik = NULL, unsigned int _linia = 0, float aWartosc = 0);
~NowaWartoscException() throw(){};
virtual string ToString() throw();
};
// //----------------------------------------------------------------------
NowaWartoscException::NowaWartoscException(string aStr, char* _plik, unsigned int _linia, float aWartosc){
WartoscException::m_ExceptionString = aStr;
WartoscException::_NR_LINI = _linia;
WartoscException::_NAZWA_PLIKU = (_plik != NULL)?(_plik):("-");
this->m_Wartosc = aWartosc;
}
//----------------------------------------------------------------------
string NowaWartoscException::ToString() throw(){
static char buff[20];
sprintf(buff, "%f", m_Wartosc ); // konversja float na char* (float to string)
string str = m_ExceptionString;
str += "wartosc :";
str += buff;
return str;
}
//======================================================================
int main(void){
float war;
cout << "Podaj wartosc od 4.0 do 9.0: "; cin >> war;
try{
if( war > 9.0 )
throw WartoscException( "Wartosc za duza", (char*)__FILE__, __LINE__);
else if( war < 0 )
throw NowaWartoscException("Wartosc poniżej zera", (char*)__FILE__, __LINE__, war);
else if( war < 4.0)
throw WartoscException( "Wartosc za mała", (char*)__FILE__, __LINE__);
cout << "Wartosc " << war << " poprawna" << endl;
}
catch( WartoscException& ex ){
cout << "-----------------------------------------------" << endl;
cout << "Wystapił wyjatek. Program został zakończony z błędem:" << endl;
cout << "Nazwa: " << ex.ToString() << endl;
cout << "Linia: " << ex.GetLine() << endl;
cout << "Plik: " << ex.GetFile() << endl;
}
return 0;
}