Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageYou 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
>
>
Post Follow-up to this message> 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
Post Follow-up to this messagecodymanix <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
Post Follow-up to this messageActually 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?
Post Follow-up to this messageThe 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
Post Follow-up to this message> 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
Post Follow-up to this messagecodymanix 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);
}
Post Follow-up to this messageThat'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 > >
Post Follow-up to this messageYou 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
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.