为什么不捕捉,抓住 [英] Why isn't the catch, catching
问题描述
我正在试图弄清楚try / catch方法。我写了这段代码,它会允许你输入你的号码,但如果你超出界限,它就无法捕捉它。有人知道为什么吗?
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
namespace SubscriptValidation.cs
{
class 程序
{
静态 void Main( string [] args)
{
int number;
int [] numbers = new int [ 10 ] { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ,< span class =code-digit> 9 , 10 };
Console.WriteLine( 请输入1到10之间的数字);
尝试
{
number = Convert.ToInt32(Console.ReadLine());
如果
(数字> = 1 || number < = 10 )
Console.WriteLine ( 您的号码是 +号码);
}
catch (IndexOutOfRangeException)
{
Console.WriteLine( 您的号码超出范围,请输入1到10之间的数字);
}
}
}
}
没有部分您的try
块中的代码将抛出IndexOutOfRangeException
。
您可能会从转换中获得
调用,但是你没有FormatException
或OverflowException
。 ToInt32catch
阻止这些例外。
此外,你在if
块内的条件没有意义:
if (number > = 1 || number < = 10 )...
所有整数大于或等于1
,或小于或等于10
。
如果它小于1
,那么它也小于10
;如果它大于10
,那么它也大于1
。
您的条件应该是:
if (number > = 1 && number < = 10 )...
除了Richard在解决方案1中所说的内容:
首先,在调试器下运行它,看看是什么例外。或者,在捕获IndexOutOfRangeException
之前添加行,或者更好的是,而不是这一行:
catch (异常异常){DoSomethingToInspectExceptionObject(exception); }
它将捕获所有内容,因为所有异常都是从System.Exception
派生的。尝试不同的输入变体;并且你很好地看到实际抛出了什么异常。
最好将此行保留为try-catch块的最后一个catch部分,以涵盖所有意外情况。首先,在上面,处理您可以在特定于此情况的表单中处理的异常,例如FormatException
:
尝试 {
string data = Console.ReadLine();
ProcessUserInput(date);
} catch (FormatException ex){
ExplainWhatIsWrongWithFormat(例如,数据);
} / * ... * /
catch (异常异常){
ShowAnyOtherExceptionInformation(exception);
}
别误会我:在这里,你 所有例外的>块传播 ,这是有道理的。在几乎所有其他情况下,请勿阻止它。最好的解决方案是:放手,不要在本地处理异常。异常应该只在所有线程的顶层堆栈框架上捕获,或者在某些特殊情况下捕获,例如Windows应用程序的主要偶数循环,但是,在简单的仅控制台应用程序中,甚至可能不需要。
你实际上不需要这么多问题。你应该更好地使用方法int.TryParse
,uint.TryParse
,但更好的字节。 TryParse
(并使用字节而不是 int ,只要它是1到10),任何事情。请参阅: https://msdn.microsoft.com /en-us/library/system.byte.tryparse%28v=vs.110%29.aspx 。
-SA
Hi, I'm trying to figure out the try/ catch methods. I wrote this code and it will allow you to enter your number but if you go out of bounds it doesn't catch it. Anyone know why?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SubscriptValidation.cs
{
class Program
{
static void Main(string[] args)
{
int number;
int[] numbers = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Console.WriteLine("Please enter a number from 1 to 10");
try
{
number = Convert.ToInt32(Console.ReadLine());
if
(number >= 1 || number <= 10)
Console.WriteLine("Your number is " + number);
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("Your number is out of range, Please enter a number from 1 to 10");
}
}
}
}
No part of the code inside yourtry
block is going to throw anIndexOutOfRangeException
.
You might get aFormatException
or anOverflowException
from theConvert.ToInt32
call, but you don't have acatch
block for either of those exceptions.
Furthermore, your condition within theif
block makes no sense:
if (number >= 1 || number <= 10) ...
All integers are either greater than or equal to1
, or less than or equal to10
.
If it's less than1
, then it's also less than10
; if it's greater than10
, then it's also greater than1
.
Your condition should be:
if (number >= 1 && number <= 10) ...
In addition to what Richard said in Solution 1:
First of all, run it under the debugger and see what is the exception. Alternatively, add the line before catchingIndexOutOfRangeException
or, better, instead of this line:
catch (Exception exception) { DoSomethingToInspectExceptionObject(exception); }
It will catch everything, because all exceptions are derived fromSystem.Exception
. Try different variants of input; and you well see what exceptions are actually thrown.
It's better to keep this line as the last catch part of your try-catch block, to cover all the unexpected. First, above, handle the exceptions which you can handle in a form specific to this situation, such asFormatException
:
try { string data = Console.ReadLine(); ProcessUserInput(date); } catch (FormatException ex) { ExplainWhatIsWrongWithFormat(ex, data); } /* ... */ catch (Exception exception) { ShowAnyOtherExceptionInformation(exception); }
Don't get me wrong: here, you block propagation of all exceptions, and it makes sense. In almost all other cases, don't block it. Best solution is: let go, don't handle exceptions locally at all. Exceptions should be all caught only on the top stack frame of all threads, or in some special cases, such as main even loop of a Windows application, but, in simple console-only application, even that might not be needed.
[EDIT]
You actually don't need to have so much of a problem. You should better use the methodint.TryParse
,uint.TryParse
, but betterbyte.TryParse
(and work with bytes instead of int, as soon as it's 1 to 10), anything. Please see: https://msdn.microsoft.com/en-us/library/system.byte.tryparse%28v=vs.110%29.aspx.
—SA
这篇关于为什么不捕捉,抓住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!