Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8091121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:57:44+00:00 2026-06-05T19:57:44+00:00

Well, I’ve just started to learn Exceptions at college, here is a code that

  • 0

Well, I’ve just started to learn Exceptions at college, here is a code that is supposed to throw an exception if an introduced value is out of a range stablished by me…
Thinking and analizing, I think there might me an error with the throws, and some guy tell me in other question that indeed I have not declared any throw…I may have stupid mistakes so sorry for the trouble
Here is my code:

    #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: the functions lanzarExcepcion() are supposed to throw the message if the inserted value is out of range

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:57:45+00:00Added an answer on June 5, 2026 at 7:57 pm

    Since you are using exceptions in the wrong way, I decided to “refactor” your code to show how it’s most usually done:

    #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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, I got problem here with opengl ES stuff (just started to learn about
Well im here because i have a problem. i have code that was created
Well, I have to revive a question that was answered here before. I've made
Well, that might be a strange question, and maybe just because I'm not familiar
Well, I just started to work on server side scripting , I chose PHP,
Well, this must be a silly one. Here below is a can-not-be-simpler code in
Well, I have this bit of code that is slowing down the program hugely
Well I am using the following code to take any old image into a
Well after much messing about I have finally got a query that gives sales
Well, I am storing variables inside of a class that will be used for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.