2012-06-15 44 views
0

嗯,我剛剛開始在大學學習異常,這是一個代碼,如果引入的值超出了我所確定的範圍,則該代碼會引發異常。 思考和分析,我認爲可能是我的投球錯誤,有些人在其他問題上告訴我,我確實沒有宣佈任何投擲......我可能有愚蠢的錯誤,所以對於麻煩 這是我的代碼: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()都應該拋出消息,如果插入的值超出範圍

+1

你沒有看到,因爲你是唯一'cout'ing「異常」的任何消息 – eqzx

+0

什麼消息,你好嗎? – dirkgently

+0

nrhine1就是這樣,當然我沒有調用任何異常類的功能,我只是修復了它 – dlvx

回答

2

由於您使用的例外在錯誤的道路,我決定以「重構」你的代碼,以顯示它是如何最通常做:

#include <iostream> 
#include <exception> 

class ExcepcionRango : public std::exception 
{ 
protected: 
    ExcepcionRango::ExcepcionRango() 
     { } 
}; 

class ExcedeRangoInferior : public ExcepcionRango 
{ 
public: 
    ExcedeRangoInferior::ExcedeRangoInferior() 
     { } 

    const char *what() const 
    { 
     return "Valor fuera de rango inferior"; 
    } 
}; 

class ExcedeRangoSuperior : public ExcepcionRango 
{ 
public: 
    ExcedeRangoSuperior::ExcedeRangoSuperior() 
     { } 

    const char *what() 
    { 
     return "valor fuera de rango superior"; 
    } 
}; 

int obtainValue(const int minimo, const int maximo) 
{ 
    int valor; 
    std::cout << "Introduzca un valor entre " << minimo << " y " << maximo << " : " << std::endl; 
    std::cin >> valor; 

    if (valor < min) 
     throw ExcedeRangoInferior(); 
    else if (valor > max) 
     throw ExcedeRangoSuperior(); 

    return valor; 
}; 

int main() 
{ 
    const int min = 3; 
    const int max = 10; 

    int valor; 

    try 
    { 
     valor = obtainValue(min, max); 
    } 
    catch (ExcepcionRango &exc) 
    { 
     std::cerr << "Exception: " << exc.what() << std::endl; 
    } 

    std::cin.get(); 
} 
+0

非常感謝!現在我有更清楚的例外事情。我顯然是錯誤的 – dlvx

+0

@JoséDelValle聽起來這是你應該接受的答案。 – jogojapan

-2

嗯,我已經修復它,現在它的工作!

#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(){ 
     return "Valor fuera de rango inferior"; 
    } 
}; 

class ExcedeRangoSuperior : public ExcepcionRango{ 
public: 
    ExcedeRangoSuperior::ExcedeRangoSuperior(){ 
    } 
    const char* lanzarExcepcion() throw(){ 
     return "valor fuera de rango superior"; 
    } 
}; 

int obtainValue(int minimo, int maximo){ 

    int valor; 
    std::cout<<"Introduzca un valor entre "<<minimo<<" y "<<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; 
    try{ 
     valor=obtainValue(min,max); 
     throw valor; 

    }catch(int){ 
     if(valor<min){ 

      std::cout<<"Exception: "<<exI->lanzarExcepcion(); 
     } 
     if(valor>max){ 

      std::cout<<"Exception: "<<exS->lanzarExcepcion(); 
     } 
     if(valor>min && valor<max){ 
      std::cout<<"Valor correcto"<<std::endl; 
     } 
     std::cin.get(); 
    } 

    delete exS; 
    delete exI; 
    std::cin.get(); 
} 
+4

-1:_請_,爲了上帝的愛,考慮@Joachim Pileborg的[答案](http://stackoverflow.com/a/11045456/21567)和[評論](http://stackoverflow.com/questions/11043578/C-例外 - 不要 - 讓 - 任何的異常#comment14447601_11043578)。 –

+0

當我讀到dirkgently的答案時,我沒有注意到他的答案。也許我一直在做錯誤的事情,但謝謝你,並感謝@Joachim的有用答案 – dlvx

0

您的代碼不會做你認爲它確實。

void fn() throw(); 

不說「函數fn引發異常」。它說:函數fn將NEVER拋出一個異常

相反,您使用throw關鍵字與值(如果它願意,你的程序將立即撥打terminate()和結束這是不太可能你想要的。)在一個函數的聲明部分:

void fn() 
{ 
    ExceptionRango e; 
    throw e; // or just "throw ExceptionRango();" 
} 

int main() 
{ 
    try 
    { 
     fn(); 
    } 
    catch(ExceptionRangoInferior &e) 
    { 
     // Caught an exception of type ExceptionRangoInferior 
    } 
    catch(ExceptionRango &e) 
    { 
     // Caught an exception of type ExceptionRango (or a subclass thereof 
     // which isn't ExceptionRangoInferior because that was already caught 
     // above 
    } 
    catch(...) 
    { 
     // Caught an exception of an unknown type 
    } 
}