我似乎无法向apear收到异常消息。任何Sugestions? [英] I cant seem to get an exception message to apear. Any Sugestions?

查看:106
本文介绍了我似乎无法向apear收到异常消息。任何Sugestions?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Module Module1

    Sub Main()
        Dim Input As String
        Console.WriteLine("If Cube Input 1")
        Try
            Input = Console.ReadLine()
            If Input = ("1") Then
                Dim Side As String
                Console.WriteLine("Input the value of the side")
                Side = Console.ReadLine()
                Dim C As Integer
                C = Side * Side * Side
                Console.WriteLine(C)
            End If
        Catch ex As Exception
            Console.WriteLine("That selection dosent exist please try again")
        End Try
        Console.Read()
    End Sub

End Module





我试图在用户输入错误的输入后显示消息,但我似乎无法让它工作。该错误确实有效,但它会给用户一个消息。我做错了什么?

推荐答案

你永远不会把错误扔到任何地方,所以它永远不会被提出。看看如何引发扔掉它们的异常。



但是......



这真的是一个滥用例外。您可以简单地使用if语句来确定用户是否输入了无效值。对此使用异常并不是例外意图。如果你可以处理某些东西,那就处理它,否则你可以抛出更高级别的异常来处理它。未处理的用户异常对您的用户来说是一个非常令人讨厌的惊喜。



除此之外,提高和处理异常是昂贵的,并且可以真正减慢您的应用程序。这可能在您正在进行的小型控制台应用程序中不可见,但是当您毕业于更大的事物时,您将真正开始后悔使用异常来验证用户输入。
You never "throw" the error anywhere, so it is never raised. Look at how to raise exceptions for throwing them.

But...

This is really an abuse of exceptions. You could simply use an "if" statement to determine if the user has inputted an invalid value. Using exceptions for this isn't what exceptions are meant to do. If you can handle something, handle it, otherwise you can throw exceptions for something higher up to handle it. Unhandled user exceptions are a really nasty surprise for your user.

On top of that, raising and handling exceptions is expensive and can really slow your application down. This might not be visible in the little console application you are doing, but when you graduate to bigger things you'll really start to regret using exceptions to validate user input.


我不得不重新排列我的代码有点但我在这里想出来的是
I had to rearrange my code a bit but i have figured it out my self here it is
Module Module1

    Sub Main()
        Dim Input As String
        Console.WriteLine("If Cube Input 1")
        Input = Console.ReadLine()
        If Input = ("1") Then
            Try
                Dim Side As String
                Console.WriteLine("Input the value of the side")
                Side = Console.ReadLine()
                Dim C As Integer
                C = Side * Side * Side
                Console.WriteLine(C)
            Catch ex As Exception
            Finally
                Console.ReadKey()
            End Try
        End If
    End Sub
End Module



我的作业要求我以这种方式使用例外


I had an assignment that required me to use exceptionin this way


这篇关于我似乎无法向apear收到异常消息。任何Sugestions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆