1

Can someone explain me why this gives unreachable code warning for *a=9; line. I'm using VS2015 preview and warning level 4

int main(){
int* a = foo();
try{
 *a = 5;
}catch(int)
{
 *a=9;
}
  return 0;
}
Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147
  • 7
    How could `*a = 5;` ever throw something that would be handled by `catch(int)`? – danielschemmel Mar 19 '15 at 06:06
  • @ghs.at: then if I add `foo();` line after `*a=5;` line, Warning goes away. How this happens? – Nayana Adassuriya Mar 19 '15 at 06:09
  • 1
    @NayanaAdassuriya It means that foo() might throw an exception. – CreativeMind Mar 19 '15 at 06:11
  • @harper it doesn't matter what `foo` does to this example – M.M Mar 19 '15 at 06:15
  • 1
    What does `int a* = ` mean? Do you mean `int *a = `? – Kirill Slatin Mar 19 '15 at 06:17
  • @harper no matter what `foo` does, `*a=9;` is unreachable (unless the program has already triggered undefined behaviour of course) – M.M Mar 19 '15 at 06:20
  • @harper no, it is unreachable if `foo()` returns `NULL`. In that case `*a = 5;` is reached, which causes undefined behaviour, and then nothing more can be said. – M.M Mar 19 '15 at 06:26
  • @ Kirill Slatin, thank for the point, corrected the question – Nayana Adassuriya Mar 19 '15 at 06:35
  • @harper If the compiler implements *an extension* that dereferencing null pointer causes an exception of type `int` to be thrown, then presumably it would stop issuing this warning message when that extension is active. Are you sure that `/EHa` causes null pointer dereference to throw an `int` ? Note that the code is not `catch(...)` . – M.M Mar 19 '15 at 06:36

2 Answers2

2

This gives unreachable code warning for *a=9 , because your compiler knows that the code in try block will never throw any kind of exception. So your catch block will never execute.

Use try/catch when code is prone to throw an exception.

CreativeMind
  • 897
  • 6
  • 19
  • @NayanaAdassuriya then the line causes undefined behaviour. And once undefined behaviour is uncorked it can never be put back in the bottle; the compiler doesn't have to plan anything sensible such as catching an exception. – M.M Mar 19 '15 at 06:14
  • @NayanaAdassuriya It will not throw an exception. It leads to Undefined Behaviour. – CreativeMind Mar 19 '15 at 06:15
  • @harper Look at here http://stackoverflow.com/questions/4573536/ehsc-vc-eha-synchronous-vs-asynchronous-exception-handling – CreativeMind Mar 19 '15 at 08:38
  • @harper Hans is not saying that. Please read last three paragraphs carefully. – CreativeMind Mar 19 '15 at 08:52
2

The reason it is unreachable is because your code fragment *a = 5 cannot throw an exception. It might give an access violation (for example if foo returns nullptr), but it will not be handled as a C++ exception.

If that is something you want, you CAN catch access violations using __try/__except, but I think you should only use this under exceptional circumstances.

https://msdn.microsoft.com/en-us/library/s58ftw19%28v=vs.80%29.aspx?f=255&MSPPError=-2147217396

Michael Holman
  • 901
  • 1
  • 10
  • 26
  • @Others See proof: [How to catch the null pointer exception?](http://stackoverflow.com/questions/1823721/how-to-catch-the-null-pointer-exception) – Kirill Slatin Mar 19 '15 at 06:24
  • It *could* be handled as a C++ exception. It is undefined behaviour, and when UB occurs,anything can happen, including throwing an exception and then proceeding as normal. (Yes, there are compilers that do this) – M.M Mar 19 '15 at 06:24
  • @harper, maybe so, but I just did a quick test in VS2013 (with default build settings and /W4) and I'm seeing unreachable code error with a foo that returns nullptr. – Michael Holman Mar 19 '15 at 06:33