Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 为什么';t尝试/捕获数字类型内的工作?_Vb.net_Try Catch - Fatal编程技术网

Vb.net 为什么';t尝试/捕获数字类型内的工作?

Vb.net 为什么';t尝试/捕获数字类型内的工作?,vb.net,try-catch,Vb.net,Try Catch,好的,伙计们,正如你们从上面的例子中看到的,我已经为错误处理设置了一个Try/Catch。我遇到了一个问题。是的,Try/Catch代码阻止输入字母(字符串),但当我输入十进制数时,它仍然接受它。为什么?如何预防这种情况?十进制数字并不意味着被接受,因为整数只接受整数 谢谢。数字类型之间存在隐式转换,因此不会触发错误。有不同的方法可以知道确切的数字类型。我想这里最好的选择是以下代码行中的某些内容: Dim mynumber as Integer 'This is the variable as

好的,伙计们,正如你们从上面的例子中看到的,我已经为错误处理设置了一个Try/Catch。我遇到了一个问题。是的,Try/Catch代码阻止输入字母(字符串),但当我输入十进制数时,它仍然接受它。为什么?如何预防这种情况?十进制数字并不意味着被接受,因为整数只接受整数


谢谢。

数字类型之间存在隐式转换,因此不会触发错误。有不同的方法可以知道确切的数字类型。我想这里最好的选择是以下代码行中的某些内容:

Dim mynumber as Integer 'This is the variable as an integer, (Integers only allow WHOLE numbers)

Console.WriteLine("Enter your number..") 'Ask to enter number
    Try
        mynumber = Console.ReadLine 'Read user input and store it
    Catch
        Console.WriteLine()
        Console.ForegroundColor = ConsoleColor.Red
        Console.WriteLine("Only whole numbers allowed! Press enter to start again!")
        Console.ResetColor()
        Console.ReadLine()
        Console.Clear()
        GoTo start
    End Try

如前所述,可以使用Integer.TryParse检查数字是否可以解析为整数。另外,您确实应该对使用严格的选项,这将帮助Visual Studio指出VB.NET代码中的问题

我的回答是:

Dim mynumber As Integer 'This is the variable as an integer, (Integers only allow WHOLE numbers)
Console.WriteLine("Enter your number..") 'Ask to enter number
If (Not Integer.TryParse(Console.ReadLine, mynumber)) Then
    Console.WriteLine()
    Console.ForegroundColor = ConsoleColor.Red
    Console.WriteLine("Only whole numbers allowed! Press enter to start again!")
    Console.ResetColor()
    Console.ReadLine()
    Console.Clear()
    GoTo start
End If

编辑:请参阅“为新项目设置选项严格的默认设置”部分。

关闭选项严格后,VB.NET会尝试像旧版本的VB一样,在类型检查方面有着非常愚蠢的规则。字符串、浮点类型和整数类型在很大程度上可以互换使用,尝试将浮点值赋给整数类型会以某种方式使其舍入(我忘记了确切的细节)。虽然有一些非常罕见的情况,除了严格地给出旧的VB6代码之外,选项严格OFF可能是有用的,但是,只有那些有足够的专家才能理解这种情况的复杂性的人,甚至应该考虑在任何新代码中使用它(甚至只在非常有用的小部件上使用)。


顺便说一句,需要注意的是,即使是VB.NET也认为从浮点值到整数类型的转换是“成功的”,如果将浮点值四舍五入到最接近的整数将产生一个可在该类型中表示的值,即使浮点值有一个在转换过程中丢失的非零小数部分。我不喜欢这种设计(我认为如果想要
Int32
最接近给定的浮点值,就应该使用这样一个函数),但事实就是如此。

你也可以使用Integer.TryParse(),如果输入有任何小数位,它将返回false。@mafafu,你能举个简单的例子吗?@varocabas,谢谢,似乎有很多小东西的代码。有没有一种更快/更简单的方法呢?@mafafu谢谢。我将把它包括在我的答案中,以说明所有选项。如果为false,
抛出新异常(“非整数!”)
这样就可以在没有布尔状态的情况下进入catch块。我是说,也许吧。无论哪种方法都有效。我认为上述情况是异常处理的一个糟糕例子<代码>尝试/捕获块应用于捕获意外错误。很明显,用户可以输入无效数据,所以这应该在
IF…THEN
块中处理。在捕获异常时,还要确保使用最特定的类型
Catch-ex-As-Exception
将只接受它遇到的任何错误
Catch ex As InvalidCastException
会更好,它表明您知道预期会发生哪种错误。@neolik,啊。。这真的很复杂!
Dim mynumber As Integer 'This is the variable as an integer, (Integers only allow WHOLE numbers)
Console.WriteLine("Enter your number..") 'Ask to enter number
If (Not Integer.TryParse(Console.ReadLine, mynumber)) Then
    Console.WriteLine()
    Console.ForegroundColor = ConsoleColor.Red
    Console.WriteLine("Only whole numbers allowed! Press enter to start again!")
    Console.ResetColor()
    Console.ReadLine()
    Console.Clear()
    GoTo start
End If
Option Strict On

Module Module1

    Sub Main()

        Dim myNumber As Integer 'This is the variable as an integer, (Integers only allow WHOLE numbers)
        Dim userInput As String
        Dim userInputValid As Boolean = False

        While Not userInputValid
            Console.Write("Enter your number.. ") 'Ask to enter number

            userInput = Console.ReadLine() 'Read user input and store it
            userInputValid = Integer.TryParse(userInput, myNumber)
            If Not userInputValid Then
                Console.ForegroundColor = ConsoleColor.Red
                Console.WriteLine("Only whole numbers allowed! Press enter to start again!")
                Console.ResetColor()
                Console.ReadLine()
                Console.Clear()
            End If

        End While

        Console.WriteLine("I got the number " & myNumber.ToString())
        Console.ReadLine()

    End Sub

End Module