473,748 Members | 2,672 Online
Bytes | Software Development & Data Engineering Community
+ 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("exce ption thrown");
}

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

// won't compile: "unreachabl e code detected"
public override int Run() {
throw new Exception("exce ption thrown");
return 1;
}
Nov 15 '05 #1
5 6758
n_o_s_p_a__m <n_**********@m ail.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("exce ption thrown");
}
Really? It doesn't do that for me. Could you give a short but complete
example where that happens?
// won't compile: "unreachabl e code detected"
public override int Run() {
return 1;
throw new Exception("exce ption thrown");
}

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


Those two are fair enough, in my view.

--
Jon Skeet - <sk***@pobox.co m>
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_**********@m ail.com> wrote in message
news:1f******** *************** ***@posting.goo gle.com...
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("exce ption thrown");
}
The above should compile.

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

// won't compile: "unreachabl e code detected"
public override int Run() {
throw new Exception("exce ption 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("Cann ot 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("exce ption thrown");
}
Compiles OK with me. No errors and no warnings.
// won't compile: "unreachabl e code detected"
public override int Run() {
return 1;
throw new Exception("exce ption 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: "unreachabl e code detected"
public override int Run() {
throw new Exception("exce ption 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("exce ption thrown");

was written something like this:

public override int Run() {
if (i == 1) {
throw new Exception("exce ption 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******** ********@TK2MSF TNGP10.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("exce ption thrown");
}
Compiles OK with me. No errors and no warnings.
// won't compile: "unreachabl e code detected"
public override int Run() {
return 1;
throw new Exception("exce ption 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: "unreachabl e code detected"
public override int Run() {
throw new Exception("exce ption thrown");
return 1;
}


Compiles with warning.

B\rgds
100

Nov 15 '05 #5
100
Hi pokemon,
throw new Exception("exce ption thrown");

was written something like this:

public override int Run() {
if (i == 1) {
throw new Exception("exce ption 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******** ********@TK2MSF TNGP10.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("exce ption thrown");
}

Compiles OK with me. No errors and no warnings.
// won't compile: "unreachabl e code detected"
public override int Run() {
return 1;
throw new Exception("exce ption 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: "unreachabl e code detected"
public override int Run() {
throw new Exception("exce ption 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
4218
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 c?
12
4127
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
2493
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 I installed Sharepoint Services I got the error and had to reboot and then everything was fine. I get it sometime when running aspx pages. It's very odd. Usually, I can just remove the application and readd it from the properties menu in the IIS...
1
1735
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 like drop downs that have no issue displaying last values in edit mode with seemingly more complicated code. when I attempt to find my textbox with
5
8088
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 thing to do. And I wonder why VB .NET keeps silence and makes such function return some default value instead. Isn't it error-prone? -- Dmitriy Lapshin
2
16075
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 all the time but it does happen. When I installed Sharepoint Services I got the error and had to reboot and then everything was fine. I get it sometime when running aspx pages. It's very odd. Usually, I can just remove the application and readd it...
0
1098
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 single value - whatever the query asks for: integer, string, double etc.
60
5055
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. consistently may make the code easier to understand for someone else, etc. Using "this." makes it immediately clear, for example, that the variable being referred to is a member of the same class and is not declared in the method as a local variable. ...
8
3285
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 socket.setdefaulttimeout(5.0) so that the sockets don't wait too long if there's no SSL server, I get
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9561
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9332
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8252
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.