bad_alloc & unreachable code
Answered
Greetings:
In the following code:
try {
auto ptr = new MyClass();
// ...
} catch (std::bad_alloc &) {
// ...
}
The 'catch' block is marked as unreachable code in Clion. Without disabling the code inspection rule, is there a way for Clion to recognized that 'new' operator may throw a std::bad_alloc and thus, the catch block is potentially reachable?
Thanks,
J-F
Please sign in to leave a comment.
Hi! Unfortunately, I can't reproduce the issue on my side:
What OS, CLion version and compiler do you use?
Hey Anna,
Thanks for replying. I noticed that the warning only shows up if there's a return statement in the try block. For example:
Omitting the return statement does not issue the unreachable code warning.
So it turns out that Clion is assuming that the return will always execute, even if a std::bad_alloc exception may be thrown.
J-F
Thanks a lot for the details! We have a related issue in our tracker: https://youtrack.jetbrains.com/issue/CPP-3094. And it was fixed in the CLion 2018.1 EAP, please try it. On my side the issue from your code was reproduced with CLion 2017.3.3 and wasn't reproduced with 2018.1 EAP.
Thanks again Anna,
I didn't even realize that 2018.1 EAP was out. Downloading right now. Thanks for the prompt response,
J-F
I can confirm that I'm not getting the problem with CLion 2018.1. Thanks!
I know this is old, but it was the nearest I got to what I see in CLion 2020.1.1 :
Here, the return statement is in the catch block.
The file is a C++ file and compiled with -fexceptions.
Here is a minimal example:
Is there any setting I might have missed?
@Sven Eden, it seems to be https://youtrack.jetbrains.com/issue/CPP-20176 which is in progress right now. Please comment or upvote the issue in order to get updates. See https://intellij-support.jetbrains.com/hc/en-us/articles/207241135-How-to-follow-YouTrack-issues-and-receive-notifications if you are not familiar with YouTrack.