473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Exception or showing erros

Hi
I have a question about using exceptions or showing error messages .I say
this by an example
first way:
if(n<0 and w<0)
MessageBox.Show("please enter positive")
else if (n=0 or w>0)
MessageBox.show("n is 0");

or second way
try
{
if(n<0 and w<0)
throw new Exception("please enter positive")
else if (n=0 or w>0)
throw new Exception("n is 0");
}
catch(Exception err)
{
MessageBox.show(err.Message);
}

thanks in advance
Nov 17 '05 #1
6 1269
You'd want to go with the message boxes in this case. Exceptions are
supposed to be "exceptional" situations that aren't otherwise easily
handled.

You want to avoid using them excessively and particularly in code that's
performance critical. There's a large performance cost in throwing an
exception that comes in part from having to unwind the stack. So, for
example, if you have a loop reading data from a large set of data, you
wouldn't want it to throw exceptions for every 4th row, say, because a bit
of the data is formatted incorrectly (unless you want to throw the exception
and end the loop). If you were to catch the exception, handle it, and then
continue reading data, the performance hit would likely be very noticeable.
Instead you'd simply discard the data or try to deal with the unexpected
formatting in the code without exceptions.

That's not to say that you should avoid exceptions, you just don't want to
be throwing them often. Generally, you want an exception to mean that
there's something wrong and unexpected taking place that the code in
question can't deal with.

Pete

"perspolis" <re*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Hi
I have a question about using exceptions or showing error messages .I say
this by an example
first way:
if(n<0 and w<0)
MessageBox.Show("please enter positive")
else if (n=0 or w>0)
MessageBox.show("n is 0");

or second way
try
{
if(n<0 and w<0)
throw new Exception("please enter positive")
else if (n=0 or w>0)
throw new Exception("n is 0");
}
catch(Exception err)
{
MessageBox.show(err.Message);
}

thanks in advance

Nov 17 '05 #2
Pete Davis <pdavis68@[nospam]hotmail.com> wrote:
You'd want to go with the message boxes in this case. Exceptions are
supposed to be "exceptional" situations that aren't otherwise easily
handled.
Well, anything which constitutes a failure for the method to be able to
fulfil its success case, IMO.
You want to avoid using them excessively and particularly in code that's
performance critical. There's a large performance cost in throwing an
exception that comes in part from having to unwind the stack. So, for
example, if you have a loop reading data from a large set of data, you
wouldn't want it to throw exceptions for every 4th row, say, because a bit
of the data is formatted incorrectly (unless you want to throw the exception
and end the loop). If you were to catch the exception, handle it, and then
continue reading data, the performance hit would likely be very noticeable.


It's possible it would be very noticeable. It depends how much
processing you do on each item.

Most people who talk about the performance hit of exceptions don't
realise just how cheap they are, and how little performance difference
they make most of the time. My laptop, for instance, can throw and
catch over 100,000 exceptions in a second. Now, If you're processing
hundreds of thousands of rows in the above situation, then yes,
exceptions will be significant.

I think those cases are rare compared with the number of cases where
people *avoid* exceptions because of the advice that "exceptions are
expensive" despite them being entirely appropriate for their particular
situations. I'm glad you gave the specifics of reading from a loop in a
situation where a lot of rows are invalid though.

See http://www.pobox.com/~skeet/csharp/exceptions.html for more on my
opinion on this :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
You mean everywhere in program that I can handle and guess user's errors
show appropriate message and in unexpected situiations use Exceptions.right?
"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:pt********************@giganews.com...
You'd want to go with the message boxes in this case. Exceptions are
supposed to be "exceptional" situations that aren't otherwise easily
handled.

You want to avoid using them excessively and particularly in code that's
performance critical. There's a large performance cost in throwing an
exception that comes in part from having to unwind the stack. So, for
example, if you have a loop reading data from a large set of data, you
wouldn't want it to throw exceptions for every 4th row, say, because a bit
of the data is formatted incorrectly (unless you want to throw the exception and end the loop). If you were to catch the exception, handle it, and then
continue reading data, the performance hit would likely be very noticeable. Instead you'd simply discard the data or try to deal with the unexpected
formatting in the code without exceptions.

That's not to say that you should avoid exceptions, you just don't want to
be throwing them often. Generally, you want an exception to mean that
there's something wrong and unexpected taking place that the code in
question can't deal with.

Pete

"perspolis" <re*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Hi
I have a question about using exceptions or showing error messages .I say this by an example
first way:
if(n<0 and w<0)
MessageBox.Show("please enter positive")
else if (n=0 or w>0)
MessageBox.show("n is 0");

or second way
try
{
if(n<0 and w<0)
throw new Exception("please enter positive")
else if (n=0 or w>0)
throw new Exception("n is 0");
}
catch(Exception err)
{
MessageBox.show(err.Message);
}

thanks in advance


Nov 17 '05 #4
thx
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Pete Davis <pdavis68@[nospam]hotmail.com> wrote:
You'd want to go with the message boxes in this case. Exceptions are
supposed to be "exceptional" situations that aren't otherwise easily
handled.
Well, anything which constitutes a failure for the method to be able to
fulfil its success case, IMO.
You want to avoid using them excessively and particularly in code that's
performance critical. There's a large performance cost in throwing an
exception that comes in part from having to unwind the stack. So, for
example, if you have a loop reading data from a large set of data, you
wouldn't want it to throw exceptions for every 4th row, say, because a bit of the data is formatted incorrectly (unless you want to throw the exception and end the loop). If you were to catch the exception, handle it, and then continue reading data, the performance hit would likely be very

noticeable.
It's possible it would be very noticeable. It depends how much
processing you do on each item.

Most people who talk about the performance hit of exceptions don't
realise just how cheap they are, and how little performance difference
they make most of the time. My laptop, for instance, can throw and
catch over 100,000 exceptions in a second. Now, If you're processing
hundreds of thousands of rows in the above situation, then yes,
exceptions will be significant.

I think those cases are rare compared with the number of cases where
people *avoid* exceptions because of the advice that "exceptions are
expensive" despite them being entirely appropriate for their particular
situations. I'm glad you gave the specifics of reading from a loop in a
situation where a lot of rows are invalid though.

See http://www.pobox.com/~skeet/csharp/exceptions.html for more on my
opinion on this :)

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

Nov 17 '05 #5
perspolis <re*****@hotmail.com> wrote:
You mean everywhere in program that I can handle and guess user's errors
show appropriate message and in unexpected situiations use Exceptions.right?


I suspect Pete doesn't mean that, although obviously I can't speak for
him.

It's not appropriate to try to provoke user interaction in any and
every class. User interaction should be kept to UI-specific classes -
you don't want it in your business logic. Personally, if business logic
ran into an error I would make the business logic throw an exception to
indicate to the layer above (whether that's a UI or not) what's wrong.
The layer above can handle that, or let the exception carry on up the
stack.

Note that it's often good to have methods which allow the UI to
validate user input *without* throwing exceptions.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
To be clear, when you are validating input parameters you are
interacting
directly with the user without the need for the extra overhead of an
intermediate
exception.

When you pass the validated input to a method that explicitly asserts
that the
preconditions must be valid, then that method throws an exception if the
input
is invalid. Sometimes it is just not possible to verify that all of a
method's pre-
conditions, invariants and post conditions are valid before calling a
method. In
this situation the exception is a method of communication between the
caller
and callee.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #7

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

Similar topics

0
by: Stephan Moeller | last post by:
Hi NG, I serialize the following class over .net remoting: using System; using System.Collections; namespace JobServerLib {
1
by: Reinhard Abel | last post by:
I have an old C-Program written with Borland 2.0. Now I like to do some changes, but I get lot of syntax Erros with my new computer. When I use the old mashine ( 286 ) it works perfectly but I can...
0
by: Stephan Moeller | last post by:
Hi NG, I serialize the following class over .net remoting: using System; using System.Collections; namespace JobServerLib {
2
by: zh.zhen | last post by:
i use try..catch in my windows service,and when some exception occurs, i write it to event logs. in webapplication ,if you use a catch to "hold" an exception, and do not throw up, the exception...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
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...
0
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...
0
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...
0
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...

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.