What is equivalent of GCC __attribute__ ((noreturn))
attribution in C#? To suppress warning about no return
statement in a method on bad execution flow. My code will throw an error by calling error thrower function. I am using Mono included in Unity3D 4.0.
Asked
Active
Viewed 1,158 times
2

eonil
- 83,476
- 81
- 317
- 516
-
this might help (?) Maybe harder with mono... http://msdn.microsoft.com/en-us/library/441722ys%28v=vs.100%29.aspx and also http://stackoverflow.com/questions/942238/list-of-pragma-warning-disable-codes-and-what-they-mean – Raphaël Althaus Jan 26 '13 at 22:23
-
Can you not just `throw` ? Or just out a throw underneath the method that already throws? – Marc Gravell Jan 26 '13 at 22:26
-
@MarcGravell First, just throwing is not an option, because I want to reduce complexity by abstracting everything into one function call like C style. Second, I don't understand what you mean latter statement. Can you explain me a little more? – eonil Jan 26 '13 at 22:28
-
@RaphaëlAlthaus And writing `#pragma` for each occurrence is not an option too because it increases code to write which is just opposite of what I want to do. – eonil Jan 26 '13 at 22:30
-
@Eonil if you know `SomeMethod` throws, but the compiler doesn't know that - then you could just `throw new Whatever()` after that call - it won't get hit anyway – Marc Gravell Jan 26 '13 at 22:30
-
@MarcGravell Unfortunately, it doesn't reduce code amount or complexity too. I know C# has nice attribute like `Conditional` which changes compiler behavior, so I am betting on method-level attribution which will make compiler to treat a function call like a `throw`. – eonil Jan 26 '13 at 22:32
-
2Write a method that returns an exception. You can have it perform side-effects as required. – Hans Passant Jan 26 '13 at 22:51
-
http://stackoverflow.com/questions/1999181/is-there-a-standard-never-returns-attribute-for-c-sharp-functions – zahir Jun 19 '14 at 16:22
1 Answers
0
The C# is smart. If you a have a method that returns a values, but you throw
an exception then it will not generate a warning.
int foo(int bar)
{
if (bar > 0) return bar + 1;
throw new NotSupportedException();
}
No warnings are generated.
However, if it did generate a warning you can use #pragma warning
. See http://msdn.microsoft.com/en-us/library/441722ys(v=vs.80).aspx

Richard Schneider
- 34,944
- 9
- 57
- 73