0

好吧,我刚开始在大学学习异常,这是一个代码,如果引入的值超出了我设定的范围,则应该抛出异常......思考和分析,我认为可能会出现错误与投掷,有些人在其他问题中告诉我,确实我没有宣布任何投掷......我可能有愚蠢的错误,很抱歉给你带来麻烦这是我的代码:

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

4

3 回答 3

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();
}
于 2012-06-15T06:09:38.580 回答
0

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

void fn() throw();

不会说“函数 fn 引发异常”。它说:函数 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
    }
}
于 2012-06-15T07:10:06.287 回答
-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();
}
于 2012-06-15T02:21:30.173 回答