What's with putting everything inside a single Try/Catch Block?
-
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 Subuggh...
-
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 Subuggh...
-
lazy programmer attitude or just throwing 0's and 1's at the screen hoping enough would stick to make the program works...
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! -
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 Subuggh...
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