Catch ex As Exception When...

My blog has moved.
You can view this post at the following address:
http://www.osherove.com/blog/2003/8/12/catch-ex-as-exception-when.html
Published Tuesday, August 12, 2003 11:39 PM by RoyOsherove
Filed under:

Comments

Tuesday, August 12, 2003 8:22 PM by Mattias Sjögren

# re: Catch ex As Exception When...

I too thought it was pretty cool when I first saw it, but frankly I've never found a use for it in real code, so I'm not sure how useful it actually is.

There's no equivalent in C#.

You don't get a DivideByZeroException because you're using the floating point division operator (/) and not the integer division one (\). Only integer division by zero throws, floating point generates NAN or infinity or something like that.
Tuesday, August 12, 2003 8:33 PM by Paul Vick

# re: Catch ex As Exception When...

Bah, Mattias beat me to it! Something to talk about in the blog at some point, I guess...
Tuesday, August 12, 2003 9:59 PM by Dave Rothgery

# re: Catch ex As Exception When...

0/0 is NaN (Not a Number). If x>0, then x/0 is Infinity ; if x<0, then x/0 is -Infinity. It's defined in the rules of IEEE (some number which I definitely don't know off the top of my head) floating point numbers somewhere.

Incidentally, you can definitely get the same behavior in C#, as per...

private void button1_Click(object sender, System.EventArgs e)
{
int x = int.Parse(textBox1.Text);
try
{
MessageBox.Show(((double) x/ (double) x).ToString());
}
catch(Exception ex)
{
this.Text = ex.Message;
}
}
Wednesday, August 13, 2003 3:20 AM by TrackBack

# Catch ex As Exception (sometimes)

Wednesday, August 13, 2003 5:39 AM by Derek Stone

# re: Catch ex As Exception When...

I think the bigger picture is you shouldn't be using exception handling to catch divide by zero errors. Logic! Logic! Logic! :)
Saturday, January 10, 2004 5:24 AM by Ashish

# re: Catch ex As Exception When...


How to show the exceptio via messegebox here: HELP

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' for Add button of clients bill form
Try
i1 = NumericUpDown2.Value
tot = tot + i1
TextBox13.Text = tot
C1 = DataSet41.Clientbill1.NewClientbill1Row()
C1("Branch_ss") = TextBox1.Text
C1("Name_s") = TextBox2.Text
C1("City_s") = TextBox3.Text
C1("Ref_s") = TextBox4.Text
C1("Billno_s") = TextBox5.Text
C1("Billdate_ss") = TextBox6.Text
C1("Estno_s") = TextBox7.Text
C1("Estdate_s") = TextBox8.Text
C1("Nameofpub_s") = TextBox9.Text
C1("Size_s") = TextBox10.Text
C1("Insertiondate_s") = TextBox11.Text
C1("Totalspace_s") = TextBox12.Text
C1("Rate_s") = NumericUpDown1.Value
C1("Grossamount_s") = NumericUpDown2.Value
C1("Total_s") = TextBox13.Text
DataSet41.Clientbill1.AddClientbill1Row(C1)
'OleDbDataAdapter1.ContinueUpdateOnError = True
' If DataSet41.HasChanges = True Then
' MsgBox("hi")
'OleDbUpdateCommand1.ExecuteNonQuery()
OleDbDataAdapter1.Update(DataSet41)
Catch ex As Exception
'MessageBox()
End Try
'End If

End Sub
Saturday, January 10, 2004 3:17 PM by Roy Osherove

# re: Catch ex As Exception When...

just use ex.ToString() or ex.Message in your messagebox.