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. Try {//code} catch{throw;} ?

Try {//code} catch{throw;} ?

Scheduled Pinned Locked Moved C#
csharpcomtoolstutorialquestion
3 Posts 3 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.
  • A Offline
    A Offline
    Adam Wimsatt
    wrote on last edited by
    #1

    Why would you do the following rather than writting this code without the try block? try { //do something that throws exception; } catch(Exception excep) { throw; } This is in a utility function so the idea is to handle the exception in the code that calls this function. Why would you write a try catch here instead of just letting the code throw the exception? Example: here[^]


    // TODO: Write code.

    J L 2 Replies Last reply
    0
    • A Adam Wimsatt

      Why would you do the following rather than writting this code without the try block? try { //do something that throws exception; } catch(Exception excep) { throw; } This is in a utility function so the idea is to handle the exception in the code that calls this function. Why would you write a try catch here instead of just letting the code throw the exception? Example: here[^]


      // TODO: Write code.

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      The only reason I can think is that at some latter point error handling code is to be put here, but for the time being the exception is just being thrown to the next callee in the stack.

      1 Reply Last reply
      0
      • A Adam Wimsatt

        Why would you do the following rather than writting this code without the try block? try { //do something that throws exception; } catch(Exception excep) { throw; } This is in a utility function so the idea is to handle the exception in the code that calls this function. Why would you write a try catch here instead of just letting the code throw the exception? Example: here[^]


        // TODO: Write code.

        L Offline
        L Offline
        LobsterDK
        wrote on last edited by
        #3

        You would do this if you needed to perform some form of cleanup that is only required if an error occures but don't want to swallow the exception at that point in the code. Catching and then immediately re-throwing without doing anything else serves no purpose though.

        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