Exception

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hi!

Is there any guidlines as to when exception is prefered to be used.
I mean that there exist some overhead when this exception is used so to use
it much is not good al all.

//Tony
 
Tony Johansson said:
Is there any guidlines as to when exception is prefered to be used.
I mean that there exist some overhead when this exception is used so to
use it much is not good al all.

As a rule, do not purposefully design your program in such a way that
the exceptions are used as a communication mechanism between a method and
its caller. The exceptions should be used for something exceptional, that is
something that you do not expect to happen during normal operation of the
program.

For instance, if you are going to read a file from disk, and the file is
supposed to always exist, then you can throw an exception in case the file
is not found at runtime. However, if it is expected that the file will
sometimes not exist, and your program contains code to handle this
situation, then you should be doing something like "if (!File.Exists...)
return false;" rather than "if (!File.Exists...) throw new Exception...;" in
order to report to the caller that the file is missing.
 
Back
Top