-1
else
{
    // figure out how to fo out of range excetpion.
    throw std::out_of_range("An exception occurred: Grade must be between 0.000000 and 100.000000");
    //throw std::out_of_range("Grade points must be between 0.000000 and 100.000000.");
}   

I get the error:

Percentage grade: 10000
terminate called after throwing an instance of 'std::out_of_range'
  what():  An exception occurred: Grade must be between 0.000000 and 100.000000"

How do I remove this?:

terminate called after throwing an instance of'std::out_of_range'
  what():"
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
Ivan Mak
  • 1
  • 1
  • 3
    FWIW, you really shouldn't use an exception. There is nothing exceptional about bad input and you should only use exceptions when something is truly exceptional. For bad inputs, you should be using error codes or just loops that wont stop until the correct data is entered. – NathanOliver Jun 11 '20 at 20:33
  • 3
    `catch` the exception. –  Jun 11 '20 at 20:33

3 Answers3

1

You can remove this:

terminate called after throwing an instance of'std::out_of_range'
  what():"

By using similar code as explained below:

int x = 10;
int y = 0;
int z = 0;

try {
    if (y == 0) throw ("Something wrong going on!"); // throwing string as error

    z = x / y;

    std::cout << z << std::endl;

} catch (const char *m) {
    std::cerr << m << std::endl; // printing error
}

You get something like:

Something wrong going on!

message. When you use a class like std::out_of_range, it'll show the information about where it actually thrown from during program execution. You can suppress it by not using the functions and directly use strings in throw statements and don't forget to catch the string for exception.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
  • 1
    "*You can suppress it by not using the functions and directly use strings in throw statements*" - that is a little misleading. Even if you throw a string, you still have to catch it, otherwise the app will be terminated. Also `std::out_of_range` is a class, not a function. – Remy Lebeau Jun 11 '20 at 21:08
0

When you throw, you need to catch...

#include <iostream>
#include <exception>
using namespace std;

struct OORException : public exception {
    const char* what () const throw () {
        return "Exception out of range: Grade must be between 0.000000 and 100.000000";
    }
};

int main () {

    // Use an application defined exception
    try
    {
        // value out of range exception.
        throw OORException();
        // throw std::out_of_range("An exception occurred: Grade must be between 0.000000 and 100.000000");
        //throw std::out_of_range("Grade points must be between 0.000000 and 100.000000.");
    }
    catch(OORException& e) {
        std::cout << "OORException caught" << std::endl;
        std::cout << e.what() << std::endl;
    }

    // use a predefined std::exception
    try
    {
        // value out of range exception.
        throw std::out_of_range("Exception occurred: Grade must be between 0.000000 and 100.000000");
    }
    catch (std::out_of_range& e)
    {
        std::cout << "Exception Out Of Range " << e.what() << std::endl;
    }
    catch (std::exception& e)
    {
        std::cout << "An exception occurred. Exception " << e.what() << std::endl;
    }
    return 0;
}

Whether you should throw/catch in this case is a different question.

ChuckCottrill
  • 4,360
  • 2
  • 24
  • 42
0

throw'ing the std::out_of_range exception is not a problem, in of itself. The real problem is that std::terminate() is called automatically to kill the calling process if a thread throws an exception and does not catch it.

So, you need to call your throwing function inside of a try block and catch the exception.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770