如何处理C#中的OverflowException?
我需要在方法mul()中处理OverflowException 但是上面的代码给出了以下错误:错误CS0161:'B.mul()':并非所有代码路径都返回值(CS0161) 我可以做些什么来修复它?如何处理C#中的OverflowException?,c#,.net,exception-handling,C#,.net,Exception Handling,我需要在方法mul()中处理OverflowException 但是上面的代码给出了以下错误:错误CS0161:'B.mul()':并非所有代码路径都返回值(CS0161) 我可以做些什么来修复它?mul()在捕获异常时不返回值。将return语句添加到catch块或方法末尾: public short mul() { try { return checked((short)(a * b)); } catch(OverflowException exc) {
mul()
在捕获异常时不返回值。将return
语句添加到catch
块或方法末尾:
public short mul()
{
try {
return checked((short)(a * b)); }
catch(OverflowException exc) {
Console.WriteLine(exc);
return 0; // or whatever
}
return 0; // this goes as well
}
mul()
捕获异常时不返回值。将return
语句添加到catch
块或方法末尾:
public short mul()
{
try {
return checked((short)(a * b)); }
catch(OverflowException exc) {
Console.WriteLine(exc);
return 0; // or whatever
}
return 0; // this goes as well
}
当抛出异常时,您将向控制台写入一些内容,但不返回任何值 您的方法返回值是
short
,因此您应该在catch
中返回一些值(因为方法应该在每个执行路径或抛出中返回一些short
值):
当抛出异常时,您将向控制台写入一些内容,但不返回任何值 您的方法返回值是
short
,因此您应该在catch
中返回一些值(因为方法应该在每个执行路径或抛出中返回一些short
值):
您必须从catch块抛出异常。例如:
catch(OverflowException exc)
{
Console.WriteLine(exc)
throw exc;
}
您必须从catch块抛出异常。例如:
catch(OverflowException exc)
{
Console.WriteLine(exc)
throw exc;
}
请不要把逻辑和用户界面混为一谈;只要把try{}catch{}
放在适当的位置,一切就会清楚:
class B
{
...
// Logic: multiply with possible Overflow exception
// Let us be nice and document the exception
///<exception cref="System.OverflowException">
///When a or (and) b are too large
///</exception>
public short mul()
{
// Do we know how to process the exception at the place?
// No. There're many reasonable responses:
// - stop execution
// - use some special/default value (e.g. -1, short.MaxValue)
// - switch to class C which operates with int (or BigInteger) etc.
// That's why we don't catch exception here
return checked((short)(a * b));
}
}
请不要把逻辑和用户界面混为一谈;只要把try{}catch{}
放在适当的位置,一切就会清楚:
class B
{
...
// Logic: multiply with possible Overflow exception
// Let us be nice and document the exception
///<exception cref="System.OverflowException">
///When a or (and) b are too large
///</exception>
public short mul()
{
// Do we know how to process the exception at the place?
// No. There're many reasonable responses:
// - stop execution
// - use some special/default value (e.g. -1, short.MaxValue)
// - switch to class C which operates with int (or BigInteger) etc.
// That's why we don't catch exception here
return checked((short)(a * b));
}
}
捕获异常后返回一个值,这意味着在某种情况下,
mul()
不返回值。如果你想一想你的代码是做什么的,很容易找到。所有的答案都提到在catch
中添加第二个返回
——你也可以在try/catch块之外有一个返回(通常是我所做的)return short.MinValue
在之后尝试…catch
或将返回类型设为ashort?
并返回null
(以避免short.MinValue
可能是有效的乘法结果的问题)。将处理您的异常,但是如果发生OverflowException
,则函数不会返回任何内容。在捕获异常后返回一个值,这意味着存在mul()
不返回值的情况。如果你想一想你的代码是做什么的,很容易找到。所有的答案都提到在catch
中添加第二个返回
——你也可以在try/catch块之外有一个返回(通常是我所做的)return short.MinValue
在之后尝试…catch
或将返回类型设为ashort?
并返回null
(以避免short.MinValue
可能是有效的乘法结果的问题)。将处理您的异常,但是,如果发生OverflowException
,则函数不会返回任何内容。这将解决OP编译问题,但仍然不是一个好的建议。它引入了检查特殊值的需要。此外,负数可能是允许的。这将解决OP编译问题,但仍然不是一个好建议。它引入了检查特殊值的需要。此外,负数可能是允许的。