473,796 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exception vs Boolean

Which approach is better:
1) Use a method that returns true/false (the method will log the exception
that lead to false using log4net for example)
OR
2) Use a method that returns void and throws an exception in case of failure?

If the second approach is to be suggested: Should .NET predefined
exceptions, NullReferenceEx ception, IndexOutOfRange Exception and so on, or my
own exceptions be thrown?

Thanks in advance,
Shehab.
Aug 13 '06
43 2119
Jon,
I note you didn't answer about
having to check a status after calling ArrayList.Add each time...
What exception you want to add to that, it is accepting every type of
object.

Scott nor either me have told that you should add exceptions or a retunr
code to every method, I think that it is understood somewhere from your
text. I agree with you that VB is more clear in this by its seperation of
void methods and non void methods with different names.

Cor
Aug 14 '06 #21
On Mon, 14 Aug 2006 13:31:22 +0200, "Cor Ligthert [MVP]"
<no************ @planet.nlwrote :
>Jon,
I note you didn't answer about
having to check a status after calling ArrayList.Add each time...
What exception you want to add to that, it is accepting every type of
object.
So what? It could throw OutOfMemoryExce ption, or ThreadAbortExce ption,
for example.

The CLR is designed to use exceptions and you would be better to
conform your code to that design paradigm. People using your code will
find it easier too :-)
--
Phil
Aug 14 '06 #22
Cor Ligthert [MVP] wrote:
I note you didn't answer about
having to check a status after calling ArrayList.Add each time...
What exception you want to add to that, it is accepting every type of
object.
ArrayList.Add can thrown an exception if the list is read-only or a
fixed size.
If you don't like that example, what about ArrayList.Inser t which takes
an index which must be validated?

Jon

Aug 14 '06 #23
What about:

dim x as double = 6 / 0

or

dim y as double = 0 / 0

What exceptions do we get there?

In VB 6.0, these would have resulted in errors (err), but because they are
(unfortunately) attempted so often, NaN and Infinity are what we get back,
rather than exceptions.

So, I don't think it's quite accurate to say that the .NET Framework is
built around exceptions occuring as a reslut of failure.

I also think you are splitting hairs here. My comments were not mean as an
absolute. Discression is always required when a decision has to be made and
my comments were made with that understanding.

If you had read my other post in the thread, as I suggested, you would have
seen my boiled down recommendation:

----------------------------------------------------------------
Silva M wrote:
From the horse mouth:
http://blogs.msdn.com/kcwalina/archi...16/396787.aspx
>Scott M wrote:
I think that article sums it up quite nicely:
>Do not use exceptions for normal execution flow.
Do use exeptions for system failures.
--------------------------------------------------------------------
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
Cor Ligthert [MVP] wrote:
> I note you didn't answer about
having to check a status after calling ArrayList.Add each time...
What exception you want to add to that, it is accepting every type of
object.

ArrayList.Add can thrown an exception if the list is read-only or a
fixed size.
If you don't like that example, what about ArrayList.Inser t which takes
an index which must be validated?

Jon

Aug 14 '06 #24
Then what is the point of IsNaN or IsNull or IsNothing?

Nothing is black and white Phil. There are *exceptions* (sorry) to every
rule. MS does not build the framework exclusively around the exception
paradigm or we wouldn't have values the Null, Nothing and NaN to check for.
"Phil" <fo*@foo.comwro te in message
news:fl******** *************** *********@4ax.c om...
On Mon, 14 Aug 2006 09:34:31 +0200, "Cor Ligthert [MVP]"
<no************ @planet.nlwrote :
>>Jon,

In my idea is the well written text from Scott, completely clear.

In VB it is easy to tell.
Never use an exception in a Sub because than giving back a Boolean or an
enum is more describing and as Scott wrote less consuming processing time.

Decide in a function if you can give a class with an error code in it.
Although in this case is in my idea throwing an exception more describing.

Translating this to C# gives than the answer on the exact OP question,
you
should never let a Void method throw an exception but use a method that
gives back a boolean (or enum).

Just my thought,

Cor

But if you follow this advice you are back to old-style programming
where you have to check the return code of every function (which of
course, nobody ever does); you increase the size of your program and
slow it down with all the extra error checking code. Conversely if you
don't throw an exception then you never pay this penalty.

As Jon said, if exceptions are being raised so often that it is
becoming a performance problem then it's not a truly exceptional
situation and some redesign is in order (cf Int32.TryParse) .
--
Phil

Aug 14 '06 #25
Sorry Greg, I was just adding to your comments for Shehab.
"Cowboy (Gregory A. Beamer)" <No************ @comcast.netNoS pamMwrote in
message news:e1******** ******@TK2MSFTN GP05.phx.gbl...
You = preacher
Me = choir

I forget who stated it (will probably remember as soon as I hit send):
"Never trust user input"!

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************** *************** *************** ****
Think outside the box!
*************** *************** *************** ****
"Scott M." <s-***@nospam.nosp amwrote in message
news:eL******** ******@TK2MSFTN GP03.phx.gbl...
>>2. If you find an exceptional condition, for example you can only accept
1-10 and someone sends in an 11, throw a custom exception

And ALWAYS validate end user input prior to accepting the data and
working with it to avoid the potential problems in the first place.


Aug 14 '06 #26
On Mon, 14 Aug 2006 17:28:37 -0400, "Scott M." <s-***@nospam.nosp am>
wrote:
>What about:

dim x as double = 6 / 0

or

dim y as double = 0 / 0

What exceptions do we get there?
Scott, none, and you wouldn't expect any. You will get +Infinity in
the first case and NaN in the second case. That is because it is in
conformance with the IEEE standard for floating point arithmetic. You
can argue about whether the standard is correct but don't expect me to
join you :-)

>In VB 6.0, these would have resulted in errors (err), but because they are
(unfortunately ) attempted so often, NaN and Infinity are what we get back,
rather than exceptions.
Anybody doing any serious mathematical work has got a lot more to
worry about than whether an exception is going to be thrown by the
code above...a brief hunt around on a few websites indicates this is a
huge and complex topic which fortunately my tired brain does not have
to grasp at the moment.

Regards,

--
Phil
Aug 14 '06 #27
Scott M. <s-***@nospam.nosp amwrote:
What about:

dim x as double = 6 / 0

or

dim y as double = 0 / 0

What exceptions do we get there?
Well, with the integer versions of course, we *do* get exceptions.
In VB 6.0, these would have resulted in errors (err), but because they are
(unfortunately) attempted so often, NaN and Infinity are what we get back,
rather than exceptions.
I don't believe it's because they're attempted so often - I believe
it's because the IEEE standard on which .NET floating point is based
defines the results to these operations.
So, I don't think it's quite accurate to say that the .NET Framework is
built around exceptions occuring as a reslut of failure.
I think it largely is - for the most part, if you give an invalid
parameter to a method, it'll throw an appropriate exception rather than
returning a status code of some description. I think if we were to list
examples following the exception rule they would be *vastly* greater
than the list of examples following the status code idea.
I also think you are splitting hairs here. My comments were not mean as an
absolute. Discression is always required when a decision has to be made and
my comments were made with that understanding.
Unfortunately it's my experience that comments aren't always taken with
that borne in mind.
If you had read my other post in the thread, as I suggested, you would have
seen my boiled down recommendation:
I don't think the article backs up your "avoid exceptions when you
can" recommendation *at all*. In particular, it categorically goes
against Cor's interpretation of your post - the very first "bullet
point" states: "Do not return error codes. Exceptions are the primary
means of reporting errors in frameworks." Either Cor's interpretation
of your post was flawed, or your point directly went against the
article. It would be nice if you'd clear up which is the case.
Compare:
"Do report execution failures by throwing exceptions. If a member
cannot successfully do what [it] is designed to do, it should be
considered an execution failure and an exception should be thrown."
and later:
"Do not use error codes because of concerns that exceptions might
affect performance negatively."

with:
"Exceptions are expensive in terms of CLR performance and so, you
should avoid throwing them when you can."

One of the primary ways people avoid throwing exceptions is by using
error codes - I don't think interpreting your post as encouraging the
use of error codes is unreasonable, but it's completely different to
the article.
I personally lean far closer to the article than any interpretation I
can put on your "avoidance" post. They seem worlds apart to me.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 14 '06 #28
Jon,

As I wrote, did I have not any problem with Scott his first reply, maybe I
should have avoided to give any own interpretation of that, while you are
now hashing the replies up with a lot of not to the subject belonging text
about what Scott wrote..

I could not find any point in your replies which made it more clear what you
want, this in opposite to the first reply from Scott, which is a newsgroup
answer, not a proof.

Maybe it would be have better if you did your own reply to Shehab instead of
splitting hairs on Scott his message, as I completely have to agree with
Scott that this is done by you.

By the way, I have more times told you that if you quote me in any way it
should be right, I don't want to be misquoted. I follow completely the line
as what Scott was writing. That you are misquoting that by your own text is
again something that shows that you use that technique.

Scott wrote in his first message the main sentence
>Exceptions are expensive in terms of CLR performance and so, you should
avoid throwing them when you can.

He showed than in the next sentence that trying to avoid them did not mean
never use them and gave an example how that than could be the best done.

That is were my additions are based on. Absolute not on this misquoting by
you where ever he wrote that in another context.
>Do not return error codes. Exceptions are the primary means of reporting
errors in frameworks."
Cor

"Jon Skeet [C# MVP]" <sk***@pobox.co mschreef in bericht
news:MP******** *************** *@msnews.micros oft.com...
Scott M. <s-***@nospam.nosp amwrote:
>What about:

dim x as double = 6 / 0

or

dim y as double = 0 / 0

What exceptions do we get there?

Well, with the integer versions of course, we *do* get exceptions.
>In VB 6.0, these would have resulted in errors (err), but because they
are
(unfortunately ) attempted so often, NaN and Infinity are what we get
back,
rather than exceptions.

I don't believe it's because they're attempted so often - I believe
it's because the IEEE standard on which .NET floating point is based
defines the results to these operations.
>So, I don't think it's quite accurate to say that the .NET Framework is
built around exceptions occuring as a reslut of failure.

I think it largely is - for the most part, if you give an invalid
parameter to a method, it'll throw an appropriate exception rather than
returning a status code of some description. I think if we were to list
examples following the exception rule they would be *vastly* greater
than the list of examples following the status code idea.
>I also think you are splitting hairs here. My comments were not mean as
an
absolute. Discression is always required when a decision has to be made
and
my comments were made with that understanding.

Unfortunately it's my experience that comments aren't always taken with
that borne in mind.
>If you had read my other post in the thread, as I suggested, you would
have
seen my boiled down recommendation:

I don't think the article backs up your "avoid exceptions when you
can" recommendation *at all*. In particular, it categorically goes
against Cor's interpretation of your post - the very first "bullet
point" states: "Do not return error codes. Exceptions are the primary
means of reporting errors in frameworks." Either Cor's interpretation
of your post was flawed, or your point directly went against the
article. It would be nice if you'd clear up which is the case.
Compare:
"Do report execution failures by throwing exceptions. If a member
cannot successfully do what [it] is designed to do, it should be
considered an execution failure and an exception should be thrown."
and later:
"Do not use error codes because of concerns that exceptions might
affect performance negatively."

with:
"Exceptions are expensive in terms of CLR performance and so, you
should avoid throwing them when you can."

One of the primary ways people avoid throwing exceptions is by using
error codes - I don't think interpreting your post as encouraging the
use of error codes is unreasonable, but it's completely different to
the article.
I personally lean far closer to the article than any interpretation I
can put on your "avoidance" post. They seem worlds apart to me.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Aug 14 '06 #29
By the way, if Scott wrote this somewhere,
>Do not return error codes. Exceptions are the primary means of reporting
errors in frameworks."
Than do I agree with that in the context of his first message as well. I was
only talking about Booleans and Enums, in general do I agree with the text
above if it cannot be done in a simple way with returning the first two
types used in my message. I never would advice to use any return code.

And to show that this can be even different, just have a look at the
DataAdapter Update where the returned integer can be used to see if all the
rows are completely updated as it should be, this beside the exceptions also
in this method (which can be set to ignore in the same method).

Cor

"Cor Ligthert [MVP]" <no************ @planet.nlschre ef in bericht
news:O4******** ********@TK2MSF TNGP04.phx.gbl. ..
Jon,

As I wrote, did I have not any problem with Scott his first reply, maybe I
should have avoided to give any own interpretation of that, while you are
now hashing the replies up with a lot of not to the subject belonging text
about what Scott wrote..

I could not find any point in your replies which made it more clear what
you want, this in opposite to the first reply from Scott, which is a
newsgroup answer, not a proof.

Maybe it would be have better if you did your own reply to Shehab instead
of splitting hairs on Scott his message, as I completely have to agree
with Scott that this is done by you.

By the way, I have more times told you that if you quote me in any way it
should be right, I don't want to be misquoted. I follow completely the
line as what Scott was writing. That you are misquoting that by your own
text is again something that shows that you use that technique.

Scott wrote in his first message the main sentence
>>Exceptions are expensive in terms of CLR performance and so, you should
avoid throwing them when you can.

He showed than in the next sentence that trying to avoid them did not mean
never use them and gave an example how that than could be the best done.

That is were my additions are based on. Absolute not on this misquoting by
you where ever he wrote that in another context.
>>Do not return error codes. Exceptions are the primary means of reporting
errors in frameworks."

Cor

"Jon Skeet [C# MVP]" <sk***@pobox.co mschreef in bericht
news:MP******** *************** *@msnews.micros oft.com...
>Scott M. <s-***@nospam.nosp amwrote:
>>What about:

dim x as double = 6 / 0

or

dim y as double = 0 / 0

What exceptions do we get there?

Well, with the integer versions of course, we *do* get exceptions.
>>In VB 6.0, these would have resulted in errors (err), but because they
are
(unfortunatel y) attempted so often, NaN and Infinity are what we get
back,
rather than exceptions.

I don't believe it's because they're attempted so often - I believe
it's because the IEEE standard on which .NET floating point is based
defines the results to these operations.
>>So, I don't think it's quite accurate to say that the .NET Framework is
built around exceptions occuring as a reslut of failure.

I think it largely is - for the most part, if you give an invalid
parameter to a method, it'll throw an appropriate exception rather than
returning a status code of some description. I think if we were to list
examples following the exception rule they would be *vastly* greater
than the list of examples following the status code idea.
>>I also think you are splitting hairs here. My comments were not mean as
an
absolute. Discression is always required when a decision has to be made
and
my comments were made with that understanding.

Unfortunatel y it's my experience that comments aren't always taken with
that borne in mind.
>>If you had read my other post in the thread, as I suggested, you would
have
seen my boiled down recommendation:

I don't think the article backs up your "avoid exceptions when you
can" recommendation *at all*. In particular, it categorically goes
against Cor's interpretation of your post - the very first "bullet
point" states: "Do not return error codes. Exceptions are the primary
means of reporting errors in frameworks." Either Cor's interpretation
of your post was flawed, or your point directly went against the
article. It would be nice if you'd clear up which is the case.
Compare:
"Do report execution failures by throwing exceptions. If a member
cannot successfully do what [it] is designed to do, it should be
considered an execution failure and an exception should be thrown."
and later:
"Do not use error codes because of concerns that exceptions might
affect performance negatively."

with:
"Exceptions are expensive in terms of CLR performance and so, you
should avoid throwing them when you can."

One of the primary ways people avoid throwing exceptions is by using
error codes - I don't think interpreting your post as encouraging the
use of error codes is unreasonable, but it's completely different to
the article.
I personally lean far closer to the article than any interpretation I
can put on your "avoidance" post. They seem worlds apart to me.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Aug 14 '06 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
3229
by: Diego F. | last post by:
I think I'll never come across that error. It happens when running code from a DLL that tries to write to disk. I added permissions in the project folder, the wwwroot and in IIS to NETWORK_SERVICE and Everyone, with Full Control to see if it's a permissions problem. The project is hosted in a Windows 2003 Server and developed from PCs in a domain, developing with Visual Studio 2005 Beta 1. -- Regards,
6
4056
by: Steve Long | last post by:
Help, I'm running VS.NET 2003 and when I try to start my application, I get the "unhandled exception" dialog instead of the IDE highlighting the offending line of code. The problem appears to be instantiating a particular class in my project but the dialog doesn't tell me what code in the class is causing the problem (and it a large class). How can I get the IDE to take me to the offending line of code? If I put this line in my...
1
4142
by: David Herbst | last post by:
Enterprise Library Jan 2006 with Visual Studio 2005 on Windows 2000 Server sp4. My custom exception formatter fails with a "Unable to handle exception: 'LoggingExceptionHandler'." exception. When I attached the debugger to the process and stepped into the code it executed without error. In both cases I was running a Debug build, the only difference in the case that works is that I attached the debugger to the same exact binaries....
4
1766
by: Shimon Sim | last post by:
I just switch my asp application to .net 2 and got this error. Any reasons why? Thank you. Server Error in '/RAFEmployee' Application. -------------------------------------------------------------------------------- The specified module could not be found. (Exception from HRESULT: 0x8007007E) Description: An unhandled exception occurred during the execution of the
0
1434
by: alf | last post by:
I have an app that was running in my local server using full trust, now I moved it to hosting company wish run in Medium trust. Then I get a Security exception (details below) Then I configured my local server to run in Medium level, trying to catch the problem in the app. The problem is that the behavior is random !! sometime all pages run succesfully, but some time I get the exception in any page. I'm trying to find a way to debug the...
5
7562
by: cozsmin | last post by:
hello , as u know wait() and notify() will not thow an exception if the method that calls them has the lock , or esle i misundrestood java :P this is the code that throws (unwanted) exceptions : (or u can download it from : http://www.geocities.com/mndt_0/files/WaitingForThread.java at this link it has the spaces )
3
12062
by: Mike | last post by:
Hi I have problem as folow: Caught Exception: System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. (machine.config) ---> System.Security.SecurityException: Request for the permission of type
0
1389
by: star111792 | last post by:
helo all i am working with JSP and EJBs. i have written a simple code for verifying username and password of user by using session beans. my problem is that i am getting the following exception: type Exception report message
4
23543
by: yogarajan | last post by:
The specified module could not be found. (Exception from HRESULT: 0x8007007E) Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.IO.FileNotFoundException: The specified module could not be found. (Exception from HRESULT: 0x8007007E) Source Error: An unhandled...
3
4564
by: chandhrakv | last post by:
Event code: 3005 Event message: An unhandled exception has occurred. Event time: 11/18/2009 3:03:53 PM Event time (UTC): 11/18/2009 9:33:53 AM Event ID: ef3014ea80424b00b1bc2e7095fffe4c Event sequence: 34707 Event occurrence: 17 Event detail code: 0 Process information:
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10449
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
10217
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...
0
10003
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
9047
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...
0
6785
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
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
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 we have to send another system

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.