By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,563 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,563 IT Pros & Developers. It's quick & easy.

"not all code paths return a value" when throwing exception, can't return a value

P: n/a
Can't compile. Does this mean that all functions that throw exceptions
must be of return type void?

examples:

// won't compile: "not all code paths return a value"
public override int Run() {
throw new Exception("exception thrown");
}

// won't compile: "unreachable code detected"
public override int Run() {
return 1;
throw new Exception("exception thrown");
}

// won't compile: "unreachable code detected"
public override int Run() {
throw new Exception("exception thrown");
return 1;
}
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
n_o_s_p_a__m <n_**********@mail.com> wrote:
Can't compile. Does this mean that all functions that throw exceptions
must be of return type void? examples:

// won't compile: "not all code paths return a value"
public override int Run() {
throw new Exception("exception thrown");
}
Really? It doesn't do that for me. Could you give a short but complete
example where that happens?
// won't compile: "unreachable code detected"
public override int Run() {
return 1;
throw new Exception("exception thrown");
}

// won't compile: "unreachable code detected"
public override int Run() {
throw new Exception("exception thrown");
return 1;
}


Those two are fair enough, in my view.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2

P: n/a
"n_o_s_p_a__m" <n_**********@mail.com> wrote in message
news:1f**************************@posting.google.c om...
Can't compile. Does this mean that all functions that throw exceptions
must be of return type void?

examples:

// won't compile: "not all code paths return a value"
public override int Run() {
throw new Exception("exception thrown");
}
The above should compile.

// won't compile: "unreachable code detected"
public override int Run() {
return 1;
throw new Exception("exception thrown");
}

// won't compile: "unreachable code detected"
public override int Run() {
throw new Exception("exception thrown");
return 1;
}

The above two should not.

The following compiles correctly, using the 1.1 compiler:

using System;

class Super
{
public virtual int Run()
{
return 0;
}
}

class Sub : Super
{
public override int Run()
{
throw new Exception("Cannot Run");
}
}
Nov 15 '05 #3

P: n/a
100
Hi n_o_s_p_a__m,
// won't compile: "not all code paths return a value"
public override int Run() {
throw new Exception("exception thrown");
}
Compiles OK with me. No errors and no warnings.
// won't compile: "unreachable code detected"
public override int Run() {
return 1;
throw new Exception("exception thrown");
}
*Unreachable code detected* is a warning. Even though the code compiles with
warning it is incorrect. The exception won't be thrown.

// won't compile: "unreachable code detected"
public override int Run() {
throw new Exception("exception thrown");
return 1;
}


Compiles with warning.

B\rgds
100
Nov 15 '05 #4

P: n/a
Sorry for troubling you with that one, it was my bad. The issue was that I
the line:

throw new Exception("exception thrown");

was written something like this:

public override int Run() {
if (i == 1) {
throw new Exception("exception thrown");
}
}

So the compiler was saying that the if statement precluded a return value
(or exception thrown) if the if condition was false.

"100" <10*@100.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi n_o_s_p_a__m,
// won't compile: "not all code paths return a value"
public override int Run() {
throw new Exception("exception thrown");
}
Compiles OK with me. No errors and no warnings.
// won't compile: "unreachable code detected"
public override int Run() {
return 1;
throw new Exception("exception thrown");
}


*Unreachable code detected* is a warning. Even though the code compiles

with warning it is incorrect. The exception won't be thrown.

// won't compile: "unreachable code detected"
public override int Run() {
throw new Exception("exception thrown");
return 1;
}


Compiles with warning.

B\rgds
100

Nov 15 '05 #5

P: n/a
100
Hi pokemon,
throw new Exception("exception thrown");

was written something like this:

public override int Run() {
if (i == 1) {
throw new Exception("exception thrown");
}
}
So the compiler was saying that the if statement precluded a return value
(or exception thrown) if the if condition was false. That is true. If the condition in the if statement is false, no exception
will be thrown and the method has to return a value.

If you dont want to return a value, just make the method void. If the method
is declared to return a value it has to do it, though.

I suppose your probelm is that you override a method and you don't want to
return a value in the overriding method. This is incorrect.

B\rgds
100
"100" <10*@100.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi n_o_s_p_a__m,
// won't compile: "not all code paths return a value"
public override int Run() {
throw new Exception("exception thrown");
}

Compiles OK with me. No errors and no warnings.
// won't compile: "unreachable code detected"
public override int Run() {
return 1;
throw new Exception("exception thrown");
}


*Unreachable code detected* is a warning. Even though the code compiles

with
warning it is incorrect. The exception won't be thrown.

// won't compile: "unreachable code detected"
public override int Run() {
throw new Exception("exception thrown");
return 1;
}


Compiles with warning.

B\rgds
100


Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.