Throwing Exception

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SK

    #1

    Throwing Exception

    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!!!.....Hel p
  • Jon Skeet [C# MVP]

    #2
    Re: Throwing Exception

    SK <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
    > 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!!!.....Hel p[/color]

    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 - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Peter Morris

      #3
      Re: Throwing Exception

      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
      CouldNotConvert FileException.

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

      YourComponent.C onvertAudioFile ();


      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


      Read or write articles on just about anything



      Comment

      Working...