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;
}
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;
}
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.
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