0

Below is the given code. What I want is that if a condition for throw is matched (here it is division by zero), the program should directly terminate after giving the kind of exception I want (here it is "denominator zero!"). Usually, this can be done just by writing throw statement (without writing catch statement) but the way it ends is disturbing as the IDE(eclipse in my case) shows a pop-up window telling the "program_name.exe stopped working" in window PC. Is there a better way to end the program after throwing the exception which should appear in the output window itself without these unusual looking pop-ups? (may be a custom exception format would be better)

#include <iostream>
#include <string>

using namespace std;

double div(double num, double denom)
{
    double result = 0.0;

    try
    {
        if(denom == 0)
        throw "denominator zero!\n";

        result = num/denom;

    }

    catch(const char* e)
    {
        cout<<e<<endl;
    }

    return result;
}




int main()
{
    double a = 5.0;
    double b = 0.0;

    cout<<div(a,b);


    return 0;
}
Ujjwal
  • 3
  • 3
  • 1
    Possible duplicate of [How to end C++ code](https://stackoverflow.com/questions/30250934/how-to-end-c-code) – francesco Jan 31 '19 at 13:00
  • This is **not** the right way to use exceptions. Exceptions are for non-local errors, that is, errors in a function that you called, or one that it called, etc. When the error and the report are in the same function, use `if ... else`. `if (demon == 0) { std::cout << "denominator zero.\n"; exit(0); }` – Pete Becker Jan 31 '19 at 14:33

2 Answers2

0

out'ing the error string to std::cerr, then

exit(0);

should do the job. Because usually, IDEs do such warnings if the return code is different from zero.

Ohnemichel
  • 324
  • 2
  • 9
  • The program shown should already have return code zero. I'm not that familiar with that popup, but I thought it meant the program was terminated by signal, not that it exited with non-zero status. – aschepler Jan 31 '19 at 13:09
  • @Ohnemichel Let's suppose, if I have a dynamic pointer initialized and not yet the delete statement is called and I use somewhere in the code in b/w exit(0), is that okay? – Ujjwal Jan 31 '19 at 13:12
  • @Ujjwal: Usually, Operating systems clean up after exit(0). And if you rely on cleanups to do stuff that effects things outside of your program's runtime, then you WILL need to wrap the interior of the main function with a try-catch statement, which calls exit(0) on catch, so all inner cleanups are processed. exit(0) will not. – Ohnemichel Jan 31 '19 at 13:50
0

The way to do this is to write a try ... catch bloc in main.

int main() {
    double a = 5.0;
    double b = 0.0;
    try {
        std::cout << div(a,b);
    } catch(const char*msg) {
        std::cout << msg << '\n';
        return EXIT_FAILURE;
    }
    return 0;
}

Now, in the div function, don't use try ... catch. Just throw the exception.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165