-
try{ } catch { }
I am having technical difficulties on my PC, so compiling and experimenting is not an option. Therefore I need to ask a question on here.
Question: For the code below. If an exception occurs at line 7 below, then what subsequent lines of code will be executed.
I want to know if (1) The values for First and Second will ever
be set to true? And (2)Will a value be returned at line 18. If so,
will the value be false.
1 bool SomeClass::SomeMethodOfClass(void)
2 {
3 bool First(false);
4 bool Second(false);
5
6 try {
7 char *ForceError=ABC; // An exception occurs here
8 int X=5l
9 Second=true;
10 }
11 catch(...)
12 {
13 // Handle the exception !
14 }
15
16 First=True; // Will First be set to true if an exception occurs.
17
18 return First;
19
20 }
-
If an exception is thrown then everything after line #7 would be ignored.
// Second == false
// First == true
Kuphryn
-
Actually
First=True;
Will be executed, unless the catch rethrows the exception, or returns.
Jeff
-
Jeff is right. But note that this is only true because you used catch with an ellipsis (...) - which catches all exceptions. If you'd defined 'catch' to trap a particular type of exception, it's possible that 'First = TRUE' might never be reached. That would depend upon the actual exception that occurred.
-
If exception is there at line 7
Second=true; is not going to execute.
about First = True;
Its really depends upon what r u doing in catch block.
Vinod