小编Har*_*rry的帖子

try/catch阻止功能限制

我希望程序的用户能够重复操作,直到他们通过输入某个字符串来指示程序停止运行.我试图通过使用以下命令输入单词"stop"来允许用户停止程序:

If (sequenceSelector.ToUpper().Contains("stop"))    
{
    //code to do stuff here
}
Run Code Online (Sandbox Code Playgroud)

目前,sequenceSelector可以访问该变量的唯一位置是封装在此try块中.

try
{                    
    int sequenceSelector = Convert.ToInt32(Console.ReadLine());

    if (sequenceSelector <=0)
    {
        throw new IndexOutOfRangeException();
    }
    String outputString = "[" + sequenceSelector.ToString() + "]: ";
    for (int i = 0; i < sequenceSelector; i++)
    {
        outputString = outputString + fibonacciSequence.GetValue(i).ToString() + ", ";
    }

    Console.WriteLine(outputString);

    return sequenceSelector;
}
Run Code Online (Sandbox Code Playgroud)

这会导致问题,因为其中一个catch块是:

catch (FormatException)
{
    Console.WriteLine("Invalid input detected! Please enter a number that is not <=0 and not > 20");
    return null; …
Run Code Online (Sandbox Code Playgroud)

c# try-catch

0
推荐指数
1
解决办法
77
查看次数

标签 统计

c# ×1

try-catch ×1