Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a standard "never returns" attribute for C# functions?

I have one method that looks like this:

void throwException(string msg) {     throw new MyException(msg); } 

Now if I write

int foo(int x, y) {     if (y == 0)         throwException("Doh!");     else         return x/y; } 

the compiler will complain about foo that "not all paths return a value".

Is there an attribute I can add to throwException to avoid that ? Something like:

[NeverReturns] void throwException(string msg) {     throw new MyException(msg); } 

I'm afraid custom attributes won't do, because for my purpose I'd need the cooperation of the compiler.

like image 880
user192472 Avatar asked Jan 04 '10 12:01

user192472


1 Answers

Why not just change it to

int foo(int x, y) {     if (y == 0)         throwException("Doh!");     return x/y; } 

This gives the same runtime results, and the compiler won't complain.

like image 141
bernhof Avatar answered Sep 21 '22 22:09

bernhof