Why c# compiler is not smart enough in the following scenario?
void ThrowEx() {
throw new Exception();
}
int Test() {
ThrowEx();
}
…Test()’: not all code paths return a value
Edit: in practice, i want to extract exception throwing logic into a separate method because i’m tired typing stuff like throw new FaultException<MyCustomFault>(new MyCustomFault(), "cannot validate the input");
It doesn’t look between methods; not least, the method could be in a different assembly and could change without rebuilds, or could be virtual, extern, abstract or partial – it would be confusing to spot only a small number of cases.
You could have the ThrowEx return “int”, and then:
which would make the compiler happy. Or use generics: