Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,447 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,194 people online right now. Registration is fast and FREE... Join Now!




Try/Catch blocks in VB 2005

 
Reply to this topicStart new topic

Try/Catch blocks in VB 2005

maggiec
23 Nov, 2007 - 10:34 PM
Post #1

New D.I.C Head
*

Joined: 17 Nov, 2007
Posts: 5


My Contributions
This is the code that I have written to have a message box come up if a nonnumeric value is put in the input box. However, when I try the program, no message box comes up with my comments in it. What am I doing wrong and how can I fix it?



CODE


Public Class FrmErrors2

    Dim I As Long

    Dim lngSum As Long


    Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnProcess.Click


        Try


            For I = txtStartLoop.Text To txtEndLoop.Text

                lngSum = (lngSum + I) * I


            Next

        Catch theException As FormatException
            MessageBox.Show("enter numeric data", "Data Entry Error", MessageBoxButtons.OK)

        End Try

        Try

        
            lblResults.Text = lngSum



            lngSum = TxtDividend.Text / TxtDivisor.Text


            lblDivideResults.Text = lngSum
        Catch theException As FormatException
            MessageBox.Show("Enter numeric data.")

        End Try

        TxtDisappears.Visible = txtSetVisible.Text


    End Sub
End Class


User is offlineProfile CardPM
+Quote Post

sharpy
RE: Try/Catch Blocks In VB 2005
27 Nov, 2007 - 12:52 PM
Post #2

D.I.C Head
Group Icon

Joined: 2 Jun, 2007
Posts: 204



Thanked: 2 times
Dream Kudos: 275
My Contributions
I find it easier to stop text being entered into the textbox in the first place.

Try this code snippet



http://www.dreamincode.net/code/snippet522.htm

Hope this helps smile.gif
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Try/Catch Blocks In VB 2005
27 Nov, 2007 - 09:34 PM
Post #3

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 4,026



Thanked: 38 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
You're probably catching the wrong exception.
User is offlineProfile CardPM
+Quote Post

MegMcD
RE: Try/Catch Blocks In VB 2005
1 Nov, 2008 - 06:49 PM
Post #4

New D.I.C Head
*

Joined: 1 Nov, 2008
Posts: 1

QUOTE(maggiec @ 23 Nov, 2007 - 11:34 PM) *

This is the code that I have written to have a message box come up if a nonnumeric value is put in the input box. However, when I try the program, no message box comes up with my comments in it. What am I doing wrong and how can I fix it?



CODE


Public Class FrmErrors2

    Dim I As Long

    Dim lngSum As Long


    Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnProcess.Click


        Try


            For I = txtStartLoop.Text To txtEndLoop.Text

                lngSum = (lngSum + I) * I


            Next

        Catch theException As FormatException
            MessageBox.Show("enter numeric data", "Data Entry Error", MessageBoxButtons.OK)

        End Try

        Try

        
            lblResults.Text = lngSum



            lngSum = TxtDividend.Text / TxtDivisor.Text


            lblDivideResults.Text = lngSum
        Catch theException As FormatException
            MessageBox.Show("Enter numeric data.")

        End Try

        TxtDisappears.Visible = txtSetVisible.Text


    End Sub
End Class





Try using InvalidCastException as your error instead of FormatException
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: Try/Catch Blocks In VB 2005
1 Nov, 2008 - 07:06 PM
Post #5

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 863



Thanked: 54 times
Dream Kudos: 2200
My Contributions
QUOTE(MegMcD @ 2 Nov, 2008 - 02:49 AM) *


Try using InvalidCastException as your error instead of FormatException

or plain old Exception
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 12:50PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month