Program execution after exception is caught

I have one fundamental question when it comes exception handling.I have written below program to convey my question clearly.I have written a program to calculate the square root of a number and I have done exception handling incase user enters negative number.My doubt here is I hear all the time I hear that program continues its execution once the exception it caught is handled.So in my program also I handled the exception.But it doesnt make any sense for the program to continue further with execution after user entered the negative number.Why programs will execute further even after they catch an exception? I am not able to visualisze.Could you please help? I ideally assume that program should abort or exit once an exception is caught orhandled because the data it needs is not given appropriately by the user.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 #include<iostream>
#include<csdtlib>
using namespace std;

bool handleinput(int inpt)
{
     try{
if (inpt > 0 || inpt ==0)
      return true
else    
    {       throw -1;  
        return false;

    }
        }

catch(int)
{     cout<<" oops,you entered negative number"<<endl;
}

}

float calcsqrt(int inp)
{
     bool ind=handleinput(inp);

   if(ind)
return sqrt(inp);

}



int main()
{    int input;
   float result; 
    cout<<"Please enter an integer"<<endl;
      cin >> input;
     result= calcsqrt(input);
 cout<<"square root of"<< input<<"is" << result<<endl;
return 0;

}
Last edited on
Why programs will execute further even after they catch an exception? I am not able to visualisze.Could you please help? I ideally assume that program should abort or exit once an exception is caught orhandled because the data it needs is not given appropriately by the user.


But that's up to the developer to decide. Not every exception from every part of my program might be fatal. It might mean simply that one action has failed, but that the software can continue to perform other operations.

You, as the programmer, must decide on the appropriate action your program should take when an exception is caught.
Topic archived. No new replies allowed.