473,785 Members | 2,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("plea se 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 1282
You'd want to go with the message boxes in this case. Exceptions are
supposed to be "exceptiona l" 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*****@hotmai l.com> wrote in message
news:%2******** ********@TK2MSF TNGP15.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("plea se 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 "exceptiona l" 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.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
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.righ t?
"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:pt******** ************@gi ganews.com...
You'd want to go with the message boxes in this case. Exceptions are
supposed to be "exceptiona l" 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*****@hotmai l.com> wrote in message
news:%2******** ********@TK2MSF TNGP15.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("plea se 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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Pete Davis <pdavis68@[nospam]hotmail.com> wrote:
You'd want to go with the message boxes in this case. Exceptions are
supposed to be "exceptiona l" 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.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

Nov 17 '05 #5
perspolis <re*****@hotmai l.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.righ t?


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.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
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
1238
by: Stephan Moeller | last post by:
Hi NG, I serialize the following class over .net remoting: using System; using System.Collections; namespace JobServerLib {
1
1660
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 not use the program on the new computer. It will not run.( Runs under MS-DOS) Is there any one how can help me? I also used newer Compilers but with the same result. (Syntax Erros) thanks
0
219
by: Stephan Moeller | last post by:
Hi NG, I serialize the following class over .net remoting: using System; using System.Collections; namespace JobServerLib {
2
1692
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 will be ignore if it is available. my windows service is a mul-thread service, which workes for a growing work-list. but some workitems of this list will make erros when services is running for it. the question is:
0
9645
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
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10153
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...
1
7500
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
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
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.