如何处理C#中的异常?
如果如何处理C#中的异常?,c#,exception,C#,Exception,如果double.Parse(string)尝试解析无效值(例如字符串而不是数字),我将尝试处理异常以避免程序崩溃。以下是我得到的: do { //asking customer the amount of shirts he would like to buy any shirts they want. numbershirtString = Console.ReadLine(); // customer types in amount of shirts they want.
double.Parse(string)
尝试解析无效值(例如字符串而不是数字),我将尝试处理异常以避免程序崩溃。以下是我得到的:
do
{
//asking customer the amount of shirts he would like to buy any shirts they want.
numbershirtString = Console.ReadLine(); // customer types in amount of shirts they want.
numbershirts = double.Parse(numbershirtString);
keepLooping = true;
if (numbershirts < 10)
{
Console.WriteLine("You would be buying " + numbershirts + " shirts");
keepLooping = false;
}
if (numbershirts > 10)
{
Console.WriteLine("You cannot order more than 10 shirts. Please try again.");
keepLooping = true;
}
} while (keepLooping);
do
{
//询问客户想要买多少衬衫。
numbershirtString=Console.ReadLine();//客户输入他们想要的衬衫数量。
numbershirts=double.Parse(numbershirtString);
keeploping=true;
如果(数字衬衫<10)
{
WriteLine(“您将要购买”+numbershirts+“衬衫”);
keeploping=false;
}
如果(数字衬衫>10)
{
Console.WriteLine(“您不能订购超过10件衬衫,请重试。”);
keeploping=true;
}
}同时(不停地跳);
谢谢你的帮助。提前谢谢你 改用double.TryParse()
。它根据结果返回true
或false
:
double val;
bool success = double.TryParse("red", out val);
if(success)
{
// val contains a parsed value
}
else
{
// could not parse
}
也许这对您来说有点高级,但如果您心情愉快,可以定义一个类来处理用户输入的解析。这样,您就可以将该逻辑与主程序分开(请参阅) 如果我们使用隐式转换运算符,事情会变得非常简单。例如,所有这些现在都是合法的:
UserEntry entry = Console.ReadLine();
if (!entry.IsInt) continue;
if (entry < 10) return entry;
注:
int
而不是double
来存储衬衫的数量10
提取到它自己的符号中,MaximumOrder
。这会让你在作业中得到几分Enter the number of shirts to order:
22
22 is too many! Please enter 10 or fewer.
Enter the number of shirts to order:
sdlfkj
You entered an invalid number.
Enter the number of shirts to order:
9
OK, I'll order 9 shirts.
要处理异常,在C语言中,与其他语言中类似,您可以使用
try..catch
块。
请看最简单的语法:
try
{
//Try to run some code.
}
catch
{
//Do something if anything excepted.
}
如果您有兴趣检索破坏代码的异常:
try
{
//Try to run some code.
}
catch (Exception ex)
{
//Do something ex was thrown.
}
如果将ex
的类型更改为继承基类Exception
的类型,则仅处理该类型的所有异常:
try
{
//Try to run some code.
}
catch (StackOverflowException ex)
{
//Do something ex was thrown because you overflowed the stack.
}
但是,我建议您使用double.TryParse(string,out-double)
方法,而不是谈论try..catch
block,您可以在Google上找到更多关于它的信息。
它的语法与double.Parse稍有不同,但实际上它以不同的方式实现了相同的功能。
如果输入有效,则返回true
,否则返回false
,而在第一个参数中,您只需传递字符串
输入,在第二个参数中,需要对结果变量进行输出引用:
double x = 0;
string number = "125.3";
if (double.TryParse(number, out x))
Console.WriteLine("Your number is " + x.ToString());
else
Console.WriteLine("Your input isn't valid");
返回值取决于输入;o) C#7.0
if(double.TryParse(s,out var i)){use i}else{做一些没有i的事情}
try
{
//Try to run some code.
}
catch (StackOverflowException ex)
{
//Do something ex was thrown because you overflowed the stack.
}
double x = 0;
string number = "125.3";
if (double.TryParse(number, out x))
Console.WriteLine("Your number is " + x.ToString());
else
Console.WriteLine("Your input isn't valid");