why inner nested try block doesn't work?

Apr 12, 2020 at 5:02am
I wrote code with a nested try block, but I don't know why the inner try block doesn't work, and the compiler doesn't show me any errors, it compiles the code but the inner one doesn't print anything

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
#include<iostream>
using namespace std;



int main()
{
	enum errorList { zeroVariableError = 4213, sVGreaterThan = 5614 };

	int number1 = 0, number2 = 3;
	int sum = number1 + number2;

	try
	{
		if (sum == number2 || sum == number1)
			throw zeroVariableError;

		try
		{
			if (number2 > number1)
				throw sVGreaterThan;
		}

		catch (errorList)
		{
			cout << "Error " << sVGreaterThan << ": second variable is greater than first one" << endl;
		}
	}

	catch (errorList)
	{
		cout << "Error " << zeroVariableError << ": one of your variable's value is zero" << endl;
	}
}



the output is only this line : Error 4213: one of your variable's value is zero
Apr 12, 2020 at 7:03am
> if (sum == number2 || sum == number1)
Number1 is zero, so the sum will always be number2
Apr 12, 2020 at 9:14am
I know this, but in the second try block it says if(number2 > number1) is true. why it doesn't throw ?
Apr 12, 2020 at 9:51am
> throw zeroVariableError;
Because this!
If it doesn't reach the inner if, what do you expect?


> int number1 = 1, number2 = 3;
$ g++ -g bar.cpp
$ ./a.out 
Error 5614: second variable is greater than first one
Apr 12, 2020 at 10:22am
Hello aminkhormaei,

I think you may be misunderstanding the try/catch. When you enter the outer try block if it should go wrong, oor in your case you throw an exception, it goes to the outer catch to handle the problem and then continues.

This should help explain it:
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
#include<iostream>
using namespace std;



int main()
{
	enum errorList { zeroVariableError = 4213, sVGreaterThan = 5614 };

	int number1 = 0, number2 = 3;
	int sum = number1 + number2;

	try
	{
                std::cout << "\nFirst try block.\n\n";
        
		if (sum == number2 || sum == number1)
			throw zeroVariableError;

                std::cout << "\nSecond try block.\n";
        
		try  // <--- You never reach here because of the first "throw".
		{
			if (number2 > number1)
				throw sVGreaterThan;
		}

		catch (errorList)
		{
			cout << "Error " << sVGreaterThan << ": second variable is greater than first one" << endl;
		}
	}

	catch (errorList)
	{
		cout << "Error " << zeroVariableError << ": one of your variable's value is zero" << endl;
	}
	
	std::cout << "\nThis is where you end up next." << '\n';
}


And the output is:

First try block.

Error 4213: one of your variable's value is zero

This is where you end up next.


Just because the inner try/catch is there the program will not back up to deal with it.

Changing "number1" to (1) I did get this:

First try block.


Second try block.

Error 5614: second variable is greater than first one

This is where you end up next.



As you have it set up I do not see a nested try/catch working.

Andy
Apr 12, 2020 at 2:26pm
@salem c @handy andy , thank you
Topic archived. No new replies allowed.