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

  • Thread starter Thread starter n_o_s_p_a__m
  • Start date Start date
N

n_o_s_p_a__m

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;
}
 
n_o_s_p_a__m said:
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.
 
n_o_s_p_a__m said:
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");
}
}
 
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
 
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.
 
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
 
Back
Top