Exception or showing erros

  • Thread starter Thread starter perspolis
  • Start date Start date
P

perspolis

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
 
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
 
Pete Davis said:
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 :)
 
You mean everywhere in program that I can handle and guess user's errors
show appropriate message and in unexpected situiations use Exceptions.right?
 
thx
Jon Skeet said:
Well, anything which constitutes a failure for the method to be able to
fulfil its success case, IMO.
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 :)
 
perspolis said:
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.
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top