Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# multiple catch blocks

C# multiple catch blocks

Scheduled Pinned Locked Moved C#
questioncsharp
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    proganon
    wrote on last edited by
    #1

    I have read many articles on expception handling in C# and I have a question regarding the use of multiple catch blocks. Is there any purpose for using multiple (specific) catch blocks if all exceptions will be handled the same way? NOTE: At the moment I am only adding exception logging to the application not exception handling.

    L V P 3 Replies Last reply
    0
    • P proganon

      I have read many articles on expception handling in C# and I have a question regarding the use of multiple catch blocks. Is there any purpose for using multiple (specific) catch blocks if all exceptions will be handled the same way? NOTE: At the moment I am only adding exception logging to the application not exception handling.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, if you want to catch *all* Exception types and threat them identically, then no there is no use in having separate catch blocks. BTW: logging is fine, not dealing with the Exception probably is a poor choice, since (unless you rethrow the Exception) the caller will be unaware something went wrong. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      P 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, if you want to catch *all* Exception types and threat them identically, then no there is no use in having separate catch blocks. BTW: logging is fine, not dealing with the Exception probably is a poor choice, since (unless you rethrow the Exception) the caller will be unaware something went wrong. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        P Offline
        P Offline
        proganon
        wrote on last edited by
        #3

        Thanks Luc. I am planning on doing something with the exception besides swallowing it...Probably rethrow, I just haven't decided yet.

        1 Reply Last reply
        0
        • P proganon

          I have read many articles on expception handling in C# and I have a question regarding the use of multiple catch blocks. Is there any purpose for using multiple (specific) catch blocks if all exceptions will be handled the same way? NOTE: At the moment I am only adding exception logging to the application not exception handling.

          V Offline
          V Offline
          Vikram A Punathambekar
          wrote on last edited by
          #4

          proganon wrote:

          Is there any purpose for using multiple (specific) catch blocks if all exceptions will be handled the same way?

          A resounding No. The only place where this would be valid is if you wanted to catch and handle CustomException1 and CustomException2 in the same way and leave the rest for calling code to handle, where said Exception types are unrelated to each other.

          Cheers, Vikram.

          Current activities: Films: Philadelphia TV series: Friends, season 4 Books: Six Thinking Hats, by Edward de Bono.


          Carpe Diem.

          1 Reply Last reply
          0
          • P proganon

            I have read many articles on expception handling in C# and I have a question regarding the use of multiple catch blocks. Is there any purpose for using multiple (specific) catch blocks if all exceptions will be handled the same way? NOTE: At the moment I am only adding exception logging to the application not exception handling.

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            proganon wrote:

            Is there any purpose for using multiple (specific) catch blocks if all exceptions will be handled the same way?

            Yes. This provides the ability to handle specific exceptions in a sensible fashion - for instance, you might have a bit of code that updates a database, and the SQL Connection terminates unexpectedly during the operation - you might want to attempt the update again (having reopened the connection). If you get a different type of exception, you probably wouldn't want to attempt the update again. There's just one example of doing this.

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups