Suppose I have a method that returns some type of object, and in that method
I have a try...catch block and just throw my own exception when I catch one.
The compiler insists that all code paths return a value, so...
do I just put
return null;
after I throw my exception in the catch block?
or should I initialize the local variable to null and return it in a finally
block?
or something else entirely, or is it just personal taste?
Thanks. 8 1834
"Daniel Billingsley" <db**********@NO.durcon.SPAAMM.com> wrote in
news:u3**************@tk2msftngp13.phx.gbl: Suppose I have a method that returns some type of object, and in that method I have a try...catch block and just throw my own exception when I catch one. The compiler insists that all code paths return a value, so...
do I just put return null; after I throw my exception in the catch block?
or should I initialize the local variable to null and return it in a finally block?
or something else entirely, or is it just personal taste?
As you say, its pretty much personal taste...
No matter how you do it, the most important thing is to document the
function so that the user (perhaps just yourself) knows that null is a
valid return value (indicating failure), and you should TEST the return
value in the calling code to see if it is null before using it.
-mbray
This doesn't seem to be a correct diagnosis of the error, since (in my
experience) a "throw" suffices as a "return" for a given code path. For
instance, this should compile (and run, and throw) with no problem:
public string ThrowsBeforeReturn() {
int x = 1;
if (x == 1) throw new Exception("Oops!");
else return "no throw";
}
-Marc
"Daniel Billingsley" <db**********@NO.durcon.SPAAMM.com> wrote in message
news:u3**************@tk2msftngp13.phx.gbl... Suppose I have a method that returns some type of object, and in that
method I have a try...catch block and just throw my own exception when I catch
one. The compiler insists that all code paths return a value, so...
do I just put return null; after I throw my exception in the catch block?
or should I initialize the local variable to null and return it in a
finally block?
or something else entirely, or is it just personal taste?
Thanks.
Daniel Billingsley wrote: Suppose I have a method that returns some type of object, and in that method I have a try...catch block and just throw my own exception when I catch one. The compiler insists that all code paths return a value, so...
do I just put return null; after I throw my exception in the catch block?
or should I initialize the local variable to null and return it in a finally block?
or something else entirely, or is it just personal taste?
Thanks.
A code path that throws an exception does not need to return anything
(in fact, it can't). You should post a sample of what you're doing and
the error you get.
--
mikeb
"Marc Lewandowski" <ma**@telebill.com> wrote in
news:OJ**************@tk2msftngp13.phx.gbl: This doesn't seem to be a correct diagnosis of the error, since (in my experience) a "throw" suffices as a "return" for a given code path. For instance, this should compile (and run, and throw) with no problem:
public string ThrowsBeforeReturn() { int x = 1; if (x == 1) throw new Exception("Oops!"); else return "no throw"; }
-Marc
You are right - I didn't quite understand the original question (or I
didn't read it correctly)... It seems, however, after reading the
original question, that there WAS a code path that wasn't returning a
value, which would indicate a possible logic error, or just
inexperienced programming. I'm guessing that they wrote code something
like:
public string NoReturnValue()
{
int x = 1;
if (x > 0) return x;
throw new Exception("Oops");
}
and never "returning a value" at the end of the function.
As far as code REQUIRING a return value, you are correct - there's
nothing wrong with the code you've presented. However, I prefer to
reserve throwing exceptions for unusual circumstances where the error is
significant enough that the code can't continue. This is different from
returning an error, which is a "possible expected outcome".
-mbray
the errors don't actually have anything to do with each other. If you
rethrow your exception in the catch it makes no difference to anything
you are returning since when the exception comes out its not going to
return a value anyway.
you shouldn't have any 'return' or anything after your throw statement
in your catch block. However, unless the throw is unavoidable in code
then you need to return something at the end of your routine.
Allen Anderson http://www.glacialcomponents.com
mailto: allen@put my website url here.com
On Wed, 15 Oct 2003 11:24:51 -0400, "Daniel Billingsley"
<db**********@NO.durcon.SPAAMM.com> wrote: Suppose I have a method that returns some type of object, and in that method I have a try...catch block and just throw my own exception when I catch one. The compiler insists that all code paths return a value, so...
do I just put return null; after I throw my exception in the catch block?
or should I initialize the local variable to null and return it in a finally block?
or something else entirely, or is it just personal taste?
Thanks.
Ah, yes you're right.
(I'm trying to work through some sample code and understand what it's doing
as I learn C# and Ado.NET)
It calls a private method in the catch block to actually throw the exception
so that a trace write can be performed in that method as well - good idea
for code reuse but not so good for this compiler constraint. I suppose the
better way to do that would be to put the Trace calls in the custom
exception's constructor overrides, eh?
"Marc Lewandowski" <ma**@telebill.com> wrote in message
news:OJ**************@tk2msftngp13.phx.gbl... This doesn't seem to be a correct diagnosis of the error, since (in my experience) a "throw" suffices as a "return" for a given code path. For instance, this should compile (and run, and throw) with no problem:
public string ThrowsBeforeReturn() { int x = 1; if (x == 1) throw new Exception("Oops!"); else return "no throw"; }
-Marc
"Daniel Billingsley" <db**********@NO.durcon.SPAAMM.com> wrote in message news:u3**************@tk2msftngp13.phx.gbl... Suppose I have a method that returns some type of object, and in that method I have a try...catch block and just throw my own exception when I catch one. The compiler insists that all code paths return a value, so...
do I just put return null; after I throw my exception in the catch block?
or should I initialize the local variable to null and return it in a finally block?
or something else entirely, or is it just personal taste?
Thanks.
Daniel Billingsley wrote: Ah, yes you're right.
(I'm trying to work through some sample code and understand what it's doing as I learn C# and Ado.NET)
It calls a private method in the catch block to actually throw the exception so that a trace write can be performed in that method as well - good idea for code reuse but not so good for this compiler constraint. I suppose the better way to do that would be to put the Trace calls in the custom exception's constructor overrides, eh?
That would be one way - possibly the best. However, if you do need to
call another method in your catch block you can cleanly work around this
problem by having that method return the exception to be thrown instead
of having it throw the exception itself:
try {
// ...
}
catch {
throw( MyPrivateExceptionLogger());
}
"Marc Lewandowski" <ma**@telebill.com> wrote in message news:OJ**************@tk2msftngp13.phx.gbl...
This doesn't seem to be a correct diagnosis of the error, since (in my experience) a "throw" suffices as a "return" for a given code path. For instance, this should compile (and run, and throw) with no problem:
public string ThrowsBeforeReturn() { int x = 1; if (x == 1) throw new Exception("Oops!"); else return "no throw"; }
-Marc
"Daniel Billingsley" <db**********@NO.durcon.SPAAMM.com> wrote in message news:u3**************@tk2msftngp13.phx.gbl...
Suppose I have a method that returns some type of object, and in that
method
I have a try...catch block and just throw my own exception when I catch
one.
The compiler insists that all code paths return a value, so...
do I just put return null; after I throw my exception in the catch block?
or should I initialize the local variable to null and return it in a
finally
block?
or something else entirely, or is it just personal taste?
Thanks.
--
mikeb
Instead of:
void ThrowMyException() { throw new Exception("my message"); }
int MyMethod() {
if (ok) return 1;
else {
ThrowMyException();
return 0; // compiler needs a return or throw here because it does
not know this will never be reached
}
}
I would suggest:
Exception MyException() { return new Exception("my message"); }
int MyMethod() {
if (ok) return 1;
else throw MyException(); // compiler is happy now
}
I don't know if this is really your problem, but it sounds like it.
Bruno.
"Daniel Billingsley" <db**********@NO.durcon.SPAAMM.com> a écrit dans le
message de news:O1****************@TK2MSFTNGP12.phx.gbl... Ah, yes you're right.
(I'm trying to work through some sample code and understand what it's
doing as I learn C# and Ado.NET)
It calls a private method in the catch block to actually throw the
exception so that a trace write can be performed in that method as well - good idea for code reuse but not so good for this compiler constraint. I suppose
the better way to do that would be to put the Trace calls in the custom exception's constructor overrides, eh?
"Marc Lewandowski" <ma**@telebill.com> wrote in message news:OJ**************@tk2msftngp13.phx.gbl... This doesn't seem to be a correct diagnosis of the error, since (in my experience) a "throw" suffices as a "return" for a given code path. For instance, this should compile (and run, and throw) with no problem:
public string ThrowsBeforeReturn() { int x = 1; if (x == 1) throw new Exception("Oops!"); else return "no throw"; }
-Marc
"Daniel Billingsley" <db**********@NO.durcon.SPAAMM.com> wrote in
message news:u3**************@tk2msftngp13.phx.gbl... Suppose I have a method that returns some type of object, and in that method I have a try...catch block and just throw my own exception when I
catch one. The compiler insists that all code paths return a value, so...
do I just put return null; after I throw my exception in the catch block?
or should I initialize the local variable to null and return it in a finally block?
or something else entirely, or is it just personal taste?
Thanks.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: jennifer johnson |
last post by:
Hello All,
I appreciate anyone's assistance. I wanted to validate user customizable
field values so I changed my JSP page so that the form action would post
existing form - action=<%=...
|
by: John Bailo |
last post by:
The c# *return* statement has been bothering me the past few months.
I don't like the fact that you can have different code paths in a method
and have multiple return statements. To me, it...
|
by: Eric the half a Bee |
last post by:
Hello
I am trying to implement a search function within a collection of
Employees. I am searching for a specific EmpID number in the collection,
and if it is found, I want to return the...
|
by: Stuart |
last post by:
Hi there
I have a stored procedure on my SQL database that retrieves a wide range of
values from about 5 different tables. My end point is to calculate the cost
against each line of retrieved...
|
by: weston |
last post by:
I've got a piece of code where, for all the world, it looks like this
fails in IE 6:
hometab = document.getElementById('hometab');
but this succeeds:
hometabemt =...
|
by: semedao |
last post by:
Hi , I am using asyc sockets p2p connection between 2 clients.
when I debug step by step the both sides , i'ts work ok.
when I run it , in somepoint (same location in the code)
when I want to...
|
by: flyingchen |
last post by:
using System;
using System.Windows.Forms;
using System.Threading;
using System.ComponentModel;
namespace ProgressControl.Core
{
public delegate object Execute(params object args);
public...
|
by: crabsdf |
last post by:
My project is a single method class which takes a xml object and, using Apache FOP, transforms it into a PDF which is returned as an output stream.
Here is full code
package...
|
by: Matt B |
last post by:
I was just wondering if there is a "best" choice from the following
couple of ways of returning a value from a method:
1)
private HashAlgorithm GetSpecificHashAlgorithm(string hashString){
if...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |