Throwing Exception

  • Thread starter Thread starter SK
  • Start date Start date
S

SK

What is the purpose of throwing exceptions in catch block.
Bcos the exception is already thrown only, it is coming to
the catch block.What is the purpose of throwing it again
then!!!.....Help
 
SK said:
What is the purpose of throwing exceptions in catch block.
Bcos the exception is already thrown only, it is coming to
the catch block.What is the purpose of throwing it again
then!!!.....Help

You may want to log the exception, but then throw it so that higher
levels can deal with it more appropriately, for example.
 
If for example you are writing an audio file conversion component, there may
be many different types of exception it would expect to encounter

1) File does not exist
2) Audio codec not installed
3) File is locked by another process

etc.


It wouldn't be reasonable to expect the user of your component to trap every
error that *you* expect to happen because these are exactly the types of
detail you are hiding from the user. So, you would trap the exception
yourself, maybe perform some kind of clean-up code, and then raise your own
CouldNotConvertFileException.

The user of your component then only needs to worry about that one exception
when trying to do

YourComponent.ConvertAudioFile();


For the sake of completeness, you can even throw your new exception and pass
the original exception as the "innerException", so the user can still see
exactly what caused the exception to start with.


--
Pete
====
Audio compression components, DIB graphics controls, FastStrings
http://www.droopyeyes.com

Read or write articles on just about anything
http://www.HowToDoThings.com
 
Back
Top