异常处理问题

时间:2017-08-10 20:47:08

标签: vb.net exception exception-handling

我在处理异常时遇到了一些困难。我对编程很新,但我现在正在编写一些与你8月10日相关的代码,以及关于你出生的十年的一些有趣的事实,当你输入你的出生日期时。每当我尝试调试时,它说我需要做一些异常处理,而我不知道那是什么。有帮助吗?我的代码如下。

Public Class Form1 'this program will tell people their age as of 8/10/2017 and will give them some fun facts about the decade they were born in.
Dim A As Integer = 2017 - TextBoxYear.Text
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Label2.Click
    If 1920 <= TextBoxYear.Text <= 1929 Then 'this section is all about sending people the fun facts. Each part defines what decade they are in, and gives fun facts.
        Console.WriteLine("You were born in the 1920's! Mohandas Karamchand Gandhi began a sweeping non-violent reform movement in India in 1920, and on August 18, 1920, women won the right to vote.")
    End If 'every decade has a little personalized message like this.
    If 1930 <= TextBoxYear.Text <= 1939 Then
        Console.WriteLine("You were born in the 1930's! World War II began in Europe in 1939 when Germany invades Poland, and the first supermarket opened in 1930.")
    End If
    If 1940 <= TextBoxYear.Text <= 1949 Then
        Console.WriteLine("You were born in the 1940's! The end of World War II in Asia occurred on August 14/15, 1945, when Japan surrendered to the Allied Powers, and Velcro was invented in 1948 in Switzerland by George deMestral.")
    End If
    If 1950 <= TextBoxYear.Text <= 1959 Then
        Console.WriteLine("You were born in the 1950's! Disneyland opened in 1955 and in Montgomery, Alabama, a bus boycott begins after Rosa Parks refuses to give up her seat to a white person.")
    End If
    If 1960 <= TextBoxYear.Text <= 1969 Then
        Console.WriteLine("You were born in the 1960's! John F. Kennedy was assassinated in 1963 in Dallas, and on August 28, 1963, Martin Luther King, Jr. gave the powerful speech now commonly known as: I have a dream.")
    End If
    If 1970 <= TextBoxYear.Text <= 1979 Then
        Console.WriteLine("You were born in the 1970's! Richard Nixon becomes the only President of the United States to ever resign from his position, and FeDex opened its doors in 1973.")
    End If
    If 1980 <= TextBoxYear.Text <= 1989 Then
        Console.WriteLine("You were born in the 1980's! In 1980, John Lennon was assassinated in New York City. In 1981, Microsoft was asked to create an operating system for IBM computers.")
    End If
    If 1990 <= TextBoxYear.Text <= 1999 Then
        Console.WriteLine("You were born in the 1990's! The 90s were a solid decade, with great movies like Pulp Fiction, great music like Radiohead and great cars like the Dodge Viper.")
    End If
    If 2000 <= TextBoxYear.Text <= 2009 Then
        Console.WriteLine("You were born in the 2000's! The first humans born in the 21st century, you were born during the decade that Obama was elected president and the first iPhone was invented.")
    End If
    If 2010 <= TextBoxYear.Text Then
        Console.WriteLine("You were born in this decade, the 2010's! If you're even old enough to read this, you should know that this is the decade of the rise of personal techology, and that this is the decade Donald Trump was elected in.")
    End If
    'This section is about finding the age of people, on the date August 10, 2017. It's better explained in my flowchart, which is attached as an image in the assignment submission.
    If 8 > TextBoxMonth.Text Then
        Console.WriteLine(A, "is your age!")
    End If
    If 8 < TextBoxMonth.Text Then
        Console.WriteLine(A - 1, "is your age!")
    End If
    If 8 = TextBoxMonth.Text Then
        If TextBoxDay.Text <= 10 Then
            Console.WriteLine(A, "is your age!")
        End If
        If TextBoxDay.Text > 10 Then
            Console.WriteLine(A - 1, "is your age!")
        End If

    End If
End Sub

End Class

谢谢!

1 个答案:

答案 0 :(得分:0)

通常我会说Stack Overflow不是一个教程网站,但我认为你可以从一些指针中受益匪浅。(你写了一个流程图!显示热情!)

我认为您的意思是说您的代码有错误,而不是例外。我想向您展示一些风格问题。它们不是大问题,但它们将来会帮助你。不过不用担心。

当你写的东西像

If 1920 <= TextBoxYear.Text  

最常见的做法是首先将对象/变量进行比较,然后将其与第二个进行比较,这样就可以写出..

If TextBoxYear.Text >=1920

这让我想到了下一点。您的原始If..Then语句不会像其他评论者提到的那样编译。为了让他们工作,你必须写一些像......

If TextBoxYear.Text >= 1920 Or TextBoxYear.Text <= 1929 Then

Visual Basic无法按照您的想法将一个对象与另一个对象进行比较。

下次使用大量If..Then语句编写代码来比较同一个对象时,使用Select..Case..End Select可能会好得多。这段代码的一个优点是它很容易结束6个月后你必须维护它。

您可以更有效地完成计算年龄的最后一段代码 - 就像这样 -

Dim dob As Date = New Date(CInt(TextBoxYear.Text), CInt(TextBoxMonth.Text), CInt(TextBoxDay.Text))
A = 2017 - dob.Year
If (dob > #2017/08/10#.AddYears(-A)) Then
    A -= 1
End If
Console.WriteLine(A & " is your age!")

但是,由于您已经编写了流程图,因此您可能希望单独保留该代码。

好的最后两件事。

您可能会注意到我在代码中添加了很多CInt(个函数。原因是TextBoxYear.Text实际上返回一个字符串 - 即使只有数字。默认情况下,Visual Studio会尝试将字符串转换为您想要的类型。在你的情况下,一个整数。但是,编写这样的代码会导致更复杂的代码出现许多问题。要解决此问题,您可以更改Visual Studio中的一个选项。它被称为Option Strict。如果启用此选项,则表示您负责在两种不同类型之间进行所有转换,例如从字符串到整数或从double到整数。它可以让你编写更多代码,但以后找到的问题会更少。

选项Strict默认为关闭。要打开它,请单击这些..

工具 - &gt;选项 - &gt;项目和解决方案 - &gt; VB默认值 - &gt;选项严格。将其设置为开。

最后!在WriteLine语句中,您写了..

A, "is your age!"

这根本不起作用。在这种情况下,Console.WriteLine需要一个字符串作为参数。要使输出成为单个字符串,您需要使用连接运算符& ..像这样

A.ToString & " is your age!"

Console.WriteLine而言,这只是一个字符串。魔术!

`.ToString'是将对象(在本例中为整数)转换为字符串的方法之一。

我希望这会有所帮助,并继续练习!