有没有办法将 try-catch 块中的值 return 转换为块本身之外的预先存在的变量?
Is there a way to return a value from a try-catch block into a preexisting variable outside of the block itself?
我觉得这可能是一个有点愚蠢的问题,但作为 C# 的初学者,我已经尝试了我目前所知道的一切。有什么方法可以 return 将一个值转换为我已经设置为在其他地方使用的值吗?或者我只是把这整件事复杂化了?每次我尝试在大括号内设置一个已经存在的变量时,我都会收到错误消息。我在下面使用的代码。
static double GetAmount()
{
double amount;
try
{
Console.WriteLine("Enter in the amount for the transaction: ");
double amount1 = Convert.ToDouble(Console.ReadLine());
return amount1;
}
catch (Exception ex)
{
bool f = true;
Console.WriteLine(ex.Message);
while (f == true)
try
{
Console.WriteLine("Enter the total in a proper format, no letters or spaces please. ");
double amount1 = Convert.ToDouble(Console.ReadLine());
f = false;
return amount1;
}
catch (Exception ex2)
{
Console.WriteLine(ex2.Message);
Console.WriteLine("Please try again.");
}
}
finally
{
return amount;
}
return amount;
}
您收到两个编译错误和一个警告。要理解它们,您必须知道 finally-block 总是在 return 从 try- 或 catch-block 之前执行。即,return amount1;
将执行 finally-block return amount;
中的语句。但是只能执行一个return-statement。因此,您收到消息:
CS0157 Control cannot leave the body of a finally clause
和
CS0165 Use of unassigned local variable 'amount'
因为在调用 return 时变量已声明但尚未赋值。
此外,您收到警告
CS0162 Unreachable code detected
在最后一行代码中,因为该方法要么被前面的 return-statement 之一留下,要么永远留在 while-loop 中。因此,最后的 return 语句永远无法执行。
布尔标志 f
是多余的。在 return 语句之前将其设置为 true
没有意义,因为该方法在 return-statement 处退出。这同时终止了 while-loop。如果你想在没有 returning 的情况下退出循环,你可以调用 break;
.
使用try-catch的简化版本:
static double GetAmount()
{
Console.Write("Enter in the amount for the transaction: ");
while (true) {
try {
double amount = Convert.ToDouble(Console.ReadLine());
return amount;
} catch (Exception ex) {
Console.WriteLine(ex.Message);
Console.Write("Enter the total in a proper format, no letters or spaces please: ");
}
}
}
语句while (true)
引入了无限循环。无穷无尽,除非它被 return
、break
或未处理的异常(或不满意的 goto 命令)留下。
更好的选择是使用不会抛出异常的TryParse
方法
static double GetAmount()
{
Console.Write("Enter the amount for the transaction: ");
while (true) {
if (Double.TryParse(Console.ReadLine(), out double amount)) {
return amount;
}
Console.Write("Enter the total in a proper format: ");
}
}
此版本与您的功能相同、安全、体积小 3 倍并且更易于阅读。
static double GetAmount()
{
double amount = 0;
try
{
Console.WriteLine("Enter in the amount for the transaction: ");
amount = Convert.ToDouble(Console.ReadLine());
}
catch (Exception ex)
{
bool f = true;
Console.WriteLine(ex.Message);
while (f)
try
{
Console.WriteLine("Enter the total in a proper format, no letters or spaces please. ");
amount = Convert.ToDouble(Console.ReadLine());
f = false;
}
catch (Exception ex2)
{
Console.WriteLine(ex2.Message);
Console.WriteLine("Please try again.");
}
}
return amount;
}
我觉得这可能是一个有点愚蠢的问题,但作为 C# 的初学者,我已经尝试了我目前所知道的一切。有什么方法可以 return 将一个值转换为我已经设置为在其他地方使用的值吗?或者我只是把这整件事复杂化了?每次我尝试在大括号内设置一个已经存在的变量时,我都会收到错误消息。我在下面使用的代码。
static double GetAmount()
{
double amount;
try
{
Console.WriteLine("Enter in the amount for the transaction: ");
double amount1 = Convert.ToDouble(Console.ReadLine());
return amount1;
}
catch (Exception ex)
{
bool f = true;
Console.WriteLine(ex.Message);
while (f == true)
try
{
Console.WriteLine("Enter the total in a proper format, no letters or spaces please. ");
double amount1 = Convert.ToDouble(Console.ReadLine());
f = false;
return amount1;
}
catch (Exception ex2)
{
Console.WriteLine(ex2.Message);
Console.WriteLine("Please try again.");
}
}
finally
{
return amount;
}
return amount;
}
您收到两个编译错误和一个警告。要理解它们,您必须知道 finally-block 总是在 return 从 try- 或 catch-block 之前执行。即,return amount1;
将执行 finally-block return amount;
中的语句。但是只能执行一个return-statement。因此,您收到消息:
CS0157 Control cannot leave the body of a finally clause
和
CS0165 Use of unassigned local variable 'amount'
因为在调用 return 时变量已声明但尚未赋值。
此外,您收到警告
CS0162 Unreachable code detected
在最后一行代码中,因为该方法要么被前面的 return-statement 之一留下,要么永远留在 while-loop 中。因此,最后的 return 语句永远无法执行。
布尔标志 f
是多余的。在 return 语句之前将其设置为 true
没有意义,因为该方法在 return-statement 处退出。这同时终止了 while-loop。如果你想在没有 returning 的情况下退出循环,你可以调用 break;
.
使用try-catch的简化版本:
static double GetAmount()
{
Console.Write("Enter in the amount for the transaction: ");
while (true) {
try {
double amount = Convert.ToDouble(Console.ReadLine());
return amount;
} catch (Exception ex) {
Console.WriteLine(ex.Message);
Console.Write("Enter the total in a proper format, no letters or spaces please: ");
}
}
}
语句while (true)
引入了无限循环。无穷无尽,除非它被 return
、break
或未处理的异常(或不满意的 goto 命令)留下。
更好的选择是使用不会抛出异常的TryParse
方法
static double GetAmount()
{
Console.Write("Enter the amount for the transaction: ");
while (true) {
if (Double.TryParse(Console.ReadLine(), out double amount)) {
return amount;
}
Console.Write("Enter the total in a proper format: ");
}
}
此版本与您的功能相同、安全、体积小 3 倍并且更易于阅读。
static double GetAmount()
{
double amount = 0;
try
{
Console.WriteLine("Enter in the amount for the transaction: ");
amount = Convert.ToDouble(Console.ReadLine());
}
catch (Exception ex)
{
bool f = true;
Console.WriteLine(ex.Message);
while (f)
try
{
Console.WriteLine("Enter the total in a proper format, no letters or spaces please. ");
amount = Convert.ToDouble(Console.ReadLine());
f = false;
}
catch (Exception ex2)
{
Console.WriteLine(ex2.Message);
Console.WriteLine("Please try again.");
}
}
return amount;
}