Try Catch Overkill

  • Thread starter Thread starter pigeonrandle
  • Start date Start date
P

pigeonrandle

Hi,
Does this bit of code represent complete overkill?!

Try

'create a treenode
Dim tn as new TreeNode()
'add it to a treeview
tv.Nodes.Add(tn)
'do some other stuff

Catch ee As Exception 'something weird has happened

Try

'try to remove the treenode from the treeview
if (tn.TreeView <> Null) tn.Remove()

Catch ee As Exception 'now things have got really dire

MessageBox.Show("Would a function like TreeNode.Remove() ever cause

a random exception that i should worry about?")

End Try

End Try



What i basically mean is ... if this situation arises then someone has
probably poured a cup of coffee into the computer so i won't have to
worry about my software/data being 'recoverable', or will this sort of
*hilarious* thing never happen?

Explanations, musings and criticism all welcome,
James Randle.
 
pigeonrandle said:
Hi,
Does this bit of code represent complete overkill?!

I'd say overkill. Your app should have some sort of general method of
catching an exception and i think this comes into that category. Have a
look at the Application.ThreadException event.

Michael
 
I did go to the trouble of converting the code to VB. Besides, it's
nice to get different people's opinions.

James.
 
pigeonrandle said:
Does this bit of code represent complete overkill?!
Try
Dim tn as new TreeNode()
tv.Nodes.Add(tn)

The only thing that you could usefully recover from is the Nodes.Add,
which means that you won't have anything to remove, so why bother?
Catch ee As Exception 'something weird has happened
Try
if (tn.TreeView <> Null) tn.Remove()

Not sure if this even works - Object references should be compared using
the Is operator; I've not come acorss "Null" in Visual Basic.
if this situation arises then someone has probably poured a cup of
coffee into the computer so i won't have to worry about my software/data
being 'recoverable', or will this sort of *hilarious* thing never happen?

I've not [yet] seen a CaffeinatedBeverageSpillageException - but there's
a /lot/ of new stuff in VB'2005... ;-)

Regards,
Phill W.
 
Phill,
Amusing. Have you looked in the System.Application.User.Stupid
namespace?

James.
pigeonrandle said:
Does this bit of code represent complete overkill?!
Try
Dim tn as new TreeNode()
tv.Nodes.Add(tn)

The only thing that you could usefully recover from is the Nodes.Add,
which means that you won't have anything to remove, so why bother?
Catch ee As Exception 'something weird has happened
Try
if (tn.TreeView <> Null) tn.Remove()

Not sure if this even works - Object references should be compared using
the Is operator; I've not come acorss "Null" in Visual Basic.
if this situation arises then someone has probably poured a cup of
coffee into the computer so i won't have to worry about my software/data
being 'recoverable', or will this sort of *hilarious* thing never happen?

I've not [yet] seen a CaffeinatedBeverageSpillageException - but there's
a /lot/ of new stuff in VB'2005... ;-)

Regards,
Phill W.
 
Back
Top