While compilers can, of course, prevent some obvious programming errors, they cannot follow best practices like this without causing some inevitable false positives along the way.
Programmers can choose to modify the contents of the exception inside the exception handler or instead introduce a completely new exception. In both cases, a warning message about an exception from the exception handler will be annoying and useless.
One case where it makes sense to change the internal state of an exception occurs when you throw an exception from a recursive function. Consider a recursive descent analyzer reporting an error from several layers along a recursive chain. Each call level can potentially add more useful information to the useful information. However, wrapping the exceptions of each subsequent layer in a new exception in this case is impractical, because in the end you get a recursive data structure, which is a flat list. One viable solution for such situations would be to create a custom exception in which each catcher can add additional information before re-throwing. Since a function (or, more precisely, a group of functions) is recursive, the place in the code from which the original exception was selected is less important than the completeness of the context, leading to the exception in the first place.
This does not mean that searching for such situations is completely useless: code checking tools such as ReSharper can certainly help programmers keep track of such problems. The compiler, however, is a poor choice for a watchdog with best practice, because for the most part the compiler should do what they say.
source share