Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Good programming style?
Is it bad style if I have a method that returns (not throw!) an exception?
It depends on the circumstances wheather I want to throw an exception or
just want to test the consistency of the data. since the my exception class
already holds all data about the problem i would just make a method like
this:

PositionIncompleteException TestComplete()
{
if (!Complete)
return new PositionIncompleteException (posID, errorString);
else
return null;
}

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu  || http://www.deutronium.tk



Report this thread to moderator Post Follow-up to this message
Old Post
codymanix
03-30-04 10:42 AM


Re: Good programming style?
You don't need to return an exception, you just throw it and then you can
catch it from a layer above. Example:

public void SomeMethod()
{
try
{
TestComplete();
}
catch(PositionIncompleteException ex)
{
// do whatever you need to do with the exception object
}
}

public void TestComplete()
{
if (!Complete)
throw new PositionIncompleteException (posID, errorString);
}


hope this helps

Fitim Skenderi


"codymanix" <no_spam_deutronium@gmx.net> wrote in message
news:udZgRjjFEHA.4012@TK2MSFTNGP09.phx.gbl...
> Is it bad style if I have a method that returns (not throw!) an exception?
> It depends on the circumstances wheather I want to throw an exception or
> just want to test the consistency of the data. since the my exception
class
> already holds all data about the problem i would just make a method like
> this:
>
> PositionIncompleteException TestComplete()
> {
>    if (!Complete)
>      return new PositionIncompleteException (posID, errorString);
>    else
>      return null;
> }
>
> --
> cody
>
> Freeware Tools, Games and Humour
> http://www.deutronium.de.vu  || http://www.deutronium.tk
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
Fitim Skenderi
03-30-04 11:37 AM


Re: Good programming style?
> You don't need to return an exception, you just throw it and then you can
> catch it from a layer above. Example:


The problem is that throwing exceptions is very very slow in .NET. I'll hope
they will fix that.
I want a quick test to run if all my positions are complete. If one of them
is not, then it is not an error.
But when I call this test in the method that serializes the data the
information _must_ be complete so i would then throw the returned exception.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu  || http://www.deutronium.tk



Report this thread to moderator Post Follow-up to this message
Old Post
codymanix
03-30-04 12:39 PM


Re: Good programming style?
codymanix <no_spam_deutronium@gmx.net> wrote: 
>
> The problem is that throwing exceptions is very very slow in .NET. I'll ho
pe
> they will fix that.

It's not *that* slow. On my box it takes about 12 seconds to
throw/catch a million exceptions. In most cases, the inefficiency of
throwing an exception isn't going to be a problem - it's just not
generally nice design.

> I want a quick test to run if all my positions are complete. If one of the
m
> is not, then it is not an error.

In that case, I'd use a test method which returns a straight boolean.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Report this thread to moderator Post Follow-up to this message
Old Post
Jon Skeet [C# MVP]
03-30-04 12:39 PM


Re: Good programming style?
Actually I must say I had noticed that throwing exceptions is very slow, may
be if you go with your return method but put a lot of commenting around it t
o make it clear?

Report this thread to moderator Post Follow-up to this message
Old Post
Martin
03-30-04 12:39 PM


Re: Good programming style?
The first time you throw an exception it may be slow to run because =

certain some new parts of your program code needs to be changed into =

native language, but this has nothing to do with the speed of the =

exception code itself.

-- =

Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
For a laugh, try web browsing with Opera's User Mode and Nostalgia enabl=
ed

Report this thread to moderator Post Follow-up to this message
Old Post
Morten Wennevik
03-30-04 02:47 PM


Re: Good programming style?
> The first time you throw an exception it may be slow to run because
> certain some new parts of your program code needs to be changed into
> native language, but this has nothing to do with the speed of the
> exception code itself.

It sometimes takes up to 4 seconds on my computer when the first exception
is thrown.
But it can be that this is only in debugging mode.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu  || http://www.deutronium.tk



Report this thread to moderator Post Follow-up to this message
Old Post
codymanix
03-30-04 02:47 PM


Re: Good programming style?
codymanix wrote:

> Is it bad style if I have a method that returns (not throw!) an exception?
> It depends on the circumstances wheather I want to throw an exception or
> just want to test the consistency of the data. since the my exception clas
s
> already holds all data about the problem i would just make a method like
> this:
>
> PositionIncompleteException TestComplete()
> {
>    if (!Complete)
>      return new PositionIncompleteException (posID, errorString);
>    else
>      return null;
> }

You might return a bool, and use two out parameters:

if (!TestComplete(out posID, out errorString)) {
DoSomethingExotic(posID, errorString);
}

or perhaps:

string errorString = TryCompleteTest(out posID);
if (errorString != null) {
DoSomethingExotic(posID, errorString);
}

or even:

class TestError
{
int posID;
string message;

// ... properties, etc.
}

TestError e = TryCompleteTest();
if (e != null) {
DoSomethingExotic(e);
}

or perhaps:

TestError e;
if (TestComplete(out e)) {
DoSomethingExotic(e);
}

Report this thread to moderator Post Follow-up to this message
Old Post
C# Learner
03-30-04 02:47 PM


Re: Good programming style?
That's running inside of the debugger, right?  There is a large and
noticeable performance hit when an exception is thrown in a debugging
session.  My guess would be that has something to do with VS.Net
intercepting the exception for debugging information.  If you run release
code outside of the debugger, you don't get such a terrible hit.

"codymanix" <no_spam_deutronium@gmx.net> wrote in message
news:uwpJislFEHA.2560@TK2MSFTNGP12.phx.gbl... 
>
> It sometimes takes up to 4 seconds on my computer when the first exception
> is thrown.
> But it can be that this is only in debugging mode.
>
> --
> cody
>
> Freeware Tools, Games and Humour
> http://www.deutronium.de.vu  || http://www.deutronium.tk
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
J.Marsch
03-30-04 03:47 PM


Re: Good programming style?
You could (I wouldnt recommend it)
but look at it this way... Consider you wish to go that route

1. Your method (TestComplete) is explicitly stating its going to have a
PositionCompleteException. So in essence it reads "The function
TestComplete is successful if it throws a PositionCompleteException".
2. You would end up checking for the exception in the calling method.
May be something like this
public void Foo() {
if(TestComplete() != null) {
//Do something
}
else {
//Do something else
}
}
Now what if you want the caller of Foo to be notified of the exception?

I'd say its not a good style at all. Hope that helps


codymanix wrote:

> Is it bad style if I have a method that returns (not throw!) an exception?
> It depends on the circumstances wheather I want to throw an exception or
> just want to test the consistency of the data. since the my exception clas
s
> already holds all data about the problem i would just make a method like
> this:
>
> PositionIncompleteException TestComplete()
> {
>    if (!Complete)
>      return new PositionIncompleteException (posID, errorString);
>    else
>      return null;
> }
>
> --
> cody
>
> Freeware Tools, Games and Humour
> http://www.deutronium.de.vu  || http://www.deutronium.tk
>
>

--
Regards,
Dilip Krishnan
MCAD, MCSD.net
dilipdotnet at apdiya dot com

Report this thread to moderator Post Follow-up to this message
Old Post
Dilip Krishnan
03-30-04 03:47 PM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

C# .NET archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:27 AM.

 

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.