嗯,我剛剛開始在大學學習異常,這是一個代碼,如果引入的值超出了我所確定的範圍,則該代碼會引發異常。 思考和分析,我認爲可能是我的投球錯誤,有些人在其他問題上告訴我,我確實沒有宣佈任何投擲......我可能有愚蠢的錯誤,所以對於麻煩 這是我的代碼:C++異常:沒有得到任何異常
#include <iostream>
#include <exception>
class ExcepcionRango : public std::exception{
protected:
ExcepcionRango::ExcepcionRango(){
}
public:
virtual const char* lanzarExcepcion()=0;
};
class ExcedeRangoInferior : public ExcepcionRango{
public:
ExcedeRangoInferior::ExcedeRangoInferior(){
}
const char* lanzarExcepcion() throw(){ //throw exception
return "Value out of minimal range";
}
};
class ExcedeRangoSuperior : public ExcepcionRango{
public:
ExcedeRangoSuperior::ExcedeRangoSuperior(){
}
const char* lanzarExcepcion() throw(){ //throw exception
return "value out of maximal range";
}
};
int obtainValue(int minimo, int maximo){ //obtain value
int valor; //value
std::cout<<"Introduce a value between "<<minimo<<" and "<<maximo<<" : "<<std::endl;
std::cin>>valor;
return valor;
};
int main(){
ExcedeRangoSuperior* exS = new ExcedeRangoSuperior();
ExcedeRangoInferior* exI= new ExcedeRangoInferior();
int min=3;
int max=10;
int valor=0; //value
try{
valor=obtainValue(min,max);
if(valor<min){
throw exS->lanzarExcepcion();
}
if(valor>max){
throw exI->lanzarExcepcion();
}
}catch(...){
std::cout<<"Exception: ";
}
delete exS;
delete exI;
std::cin.get();
}
PD:功能lanzarExcepcion()都應該拋出消息,如果插入的值超出範圍
你沒有看到,因爲你是唯一'cout'ing「異常」的任何消息 – eqzx
什麼消息,你好嗎? – dirkgently
nrhine1就是這樣,當然我沒有調用任何異常類的功能,我只是修復了它 – dlvx