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. Other Discussions
  3. The Soapbox
  4. What's with putting everything inside a single Try/Catch Block?

What's with putting everything inside a single Try/Catch Block?

Scheduled Pinned Locked Moved The Soapbox
xmlhelpquestionlounge
4 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.
  • W Offline
    W Offline
    William Winner
    wrote on last edited by
    #1

    Seriously...what does it really tell you if you put an entire sub or function within a Try/Catch block? You should be able to tell which sections of code could possibly throw an exception and why wouldn't you separate those out so that you can tell which section of code actually went wrong instead of having to track it down by the line number? This was just posted on the VB message board...

    Private Sub populateTreeview()
    Try
    'Just a good practice -- change the cursor to a
    'wait cursor while the nodes populate
    Me.Cursor = Cursors.WaitCursor
    'First, we'll load the Xml document
    Dim xDoc As New XmlDocument()
    xDoc.Load(SelectTextFile())
    'Now, clear out the treeview,
    'and add the first (root) node
    tvXmlTree.Nodes.Clear()
    tvXmlTree.Nodes.Add(New TreeNode(xDoc.DocumentElement.Name))
    Dim tNode As New TreeNode()
    tNode = DirectCast(tvXmlTree.Nodes(0), TreeNode)
    'We make a call to addTreeNode,
    'where we'll add all of our nodes
    addTreeNode(xDoc.DocumentElement, tNode)
    'Expand the treeview to show all nodes
    tvXmlTree.CollapseAll()
    Catch xExc As XmlException
    'Exception is thrown is there is an error in the Xml
    MessageBox.Show(xExc.Message)
    Catch ex As Exception
    'General exception
    MessageBox.Show(ex.Message)
    Finally
    'Change the cursor back
    Me.Cursor = Cursors.[Default]
    End Try
    End Sub

    uggh...

    R J 2 Replies Last reply
    0
    • W William Winner

      Seriously...what does it really tell you if you put an entire sub or function within a Try/Catch block? You should be able to tell which sections of code could possibly throw an exception and why wouldn't you separate those out so that you can tell which section of code actually went wrong instead of having to track it down by the line number? This was just posted on the VB message board...

      Private Sub populateTreeview()
      Try
      'Just a good practice -- change the cursor to a
      'wait cursor while the nodes populate
      Me.Cursor = Cursors.WaitCursor
      'First, we'll load the Xml document
      Dim xDoc As New XmlDocument()
      xDoc.Load(SelectTextFile())
      'Now, clear out the treeview,
      'and add the first (root) node
      tvXmlTree.Nodes.Clear()
      tvXmlTree.Nodes.Add(New TreeNode(xDoc.DocumentElement.Name))
      Dim tNode As New TreeNode()
      tNode = DirectCast(tvXmlTree.Nodes(0), TreeNode)
      'We make a call to addTreeNode,
      'where we'll add all of our nodes
      addTreeNode(xDoc.DocumentElement, tNode)
      'Expand the treeview to show all nodes
      tvXmlTree.CollapseAll()
      Catch xExc As XmlException
      'Exception is thrown is there is an error in the Xml
      MessageBox.Show(xExc.Message)
      Catch ex As Exception
      'General exception
      MessageBox.Show(ex.Message)
      Finally
      'Change the cursor back
      Me.Cursor = Cursors.[Default]
      End Try
      End Sub

      uggh...

      R Offline
      R Offline
      randprin
      wrote on last edited by
      #2

      lazy programmer attitude or just throwing 0's and 1's at the screen hoping enough would stick to make the program works...

      V 1 Reply Last reply
      0
      • R randprin

        lazy programmer attitude or just throwing 0's and 1's at the screen hoping enough would stick to make the program works...

        V Offline
        V Offline
        Vasudevan Deepak Kumar
        wrote on last edited by
        #3

        Actually ascending up the programming ladder, it is also expected to use more precise exception classes rather than just more generic System.Exception. Also, programmers are lazy sometimes to define the variable name as e and/or ee and the ignore it only to elicit a warning from the compiler like identifier not used.

        Vasudevan Deepak Kumar Personal Homepage
        Tech Gossips
        The woods are lovely, dark and deep, But I have promises to keep, And miles to go before I sleep, And miles to go before I sleep!

        1 Reply Last reply
        0
        • W William Winner

          Seriously...what does it really tell you if you put an entire sub or function within a Try/Catch block? You should be able to tell which sections of code could possibly throw an exception and why wouldn't you separate those out so that you can tell which section of code actually went wrong instead of having to track it down by the line number? This was just posted on the VB message board...

          Private Sub populateTreeview()
          Try
          'Just a good practice -- change the cursor to a
          'wait cursor while the nodes populate
          Me.Cursor = Cursors.WaitCursor
          'First, we'll load the Xml document
          Dim xDoc As New XmlDocument()
          xDoc.Load(SelectTextFile())
          'Now, clear out the treeview,
          'and add the first (root) node
          tvXmlTree.Nodes.Clear()
          tvXmlTree.Nodes.Add(New TreeNode(xDoc.DocumentElement.Name))
          Dim tNode As New TreeNode()
          tNode = DirectCast(tvXmlTree.Nodes(0), TreeNode)
          'We make a call to addTreeNode,
          'where we'll add all of our nodes
          addTreeNode(xDoc.DocumentElement, tNode)
          'Expand the treeview to show all nodes
          tvXmlTree.CollapseAll()
          Catch xExc As XmlException
          'Exception is thrown is there is an error in the Xml
          MessageBox.Show(xExc.Message)
          Catch ex As Exception
          'General exception
          MessageBox.Show(ex.Message)
          Finally
          'Change the cursor back
          Me.Cursor = Cursors.[Default]
          End Try
          End Sub

          uggh...

          J Offline
          J Offline
          Jorgen Sigvardsson
          wrote on last edited by
          #4

          Unless this function is used as an event handler, the catch is totally useless. Calling UI routines "far" from event handling routines is just poor programming. This VB code is perhaps a good example of how you wouldn't write reusable code. :)

          -- Kein Mitleid Für Die Mehrheit

          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