473,486 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
5 6728
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

72
4119
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and...
12
4099
by: Jose Fernandez | last post by:
Hello. I'm building a web service and I get this error. NEWS.News.CoverNews(string)': not all code paths return a value This is the WebMethod public SqlDataReader CoverNews(string Sport)...
0
2471
by: Jas Shultz | last post by:
I'm using Win2K3 Enterprise edition with the latest .NET framework installed. I have this problem with getting "out of disk space" errors. It doesn't happen all the time but it does happen. When...
1
1717
by: jason | last post by:
I've seen a few posts on this issue, but no clear solutions. I have a mulitiline textbox inside a datagrid. I use TemplateColumn to define as multiline with 3 rows. I have other field types...
5
8062
by: Dmitriy Lapshin [C# / .NET MVP] | last post by:
Hi all, I think the VB .NET compiler should at least issue a warning when a function does not return value. C# and C++ compilers treat this situation as an error and I believe this is the right...
2
16043
by: Jas Shultz | last post by:
I'm using Win2K3 Enterprise edition with the latest .NET framework installed. I have this problem with getting "out of disk space" errors. I have 35 Gigs of disk space free. It doesn't happen...
0
1079
by: Thomas W | last post by:
Hello, I'm trying to call a function from a C++ dll which is declared as VARIANT DLLEXPORT ExecuteFunction (LPCSTR); This function (which I can't change) executes a SQL query and returns a...
60
4968
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this....
8
3274
by: John Nagle | last post by:
Here's a wierd problem: I have a little test case for M2Crypto, which just opens up SSL connections to web servers and reads their certificates. This works fine. But if I execute ...
0
7094
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.