By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,137 Members | 2,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,137 IT Pros & Developers. It's quick & easy.

Throwing Exception

P: n/a
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
Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
SK <an*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

P: n/a
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
Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.