C ++异常:不会出现任何异常

时间:2012-06-15 01:33:37

标签: c++ exception

好吧,我刚刚开始在大学学习例外,这里有一个代码,如果引入的值超出我所建立的范围,它应该抛出异常...... 思考和分析,我想可能有一个错误的投掷,有些人告诉我其他问题,我确实没有宣布任何投掷...我可能有愚蠢的错误,所以抱歉的麻烦 这是我的代码:

    #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()应该抛出消息

3 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:0)

您的代码没有按照您的想法执行。

void fn() throw();

不说&#34;函数fn抛出异常。&#34;它说:函数fn将从不抛出异常(如果可以,你的程序会立即调用terminate()并结束。你不太可能想要那个。)

相反,您使用throw关键字将值作为函数中语句的一部分:

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
    }
}

答案 2 :(得分:-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();
}