是的,这是一段微不足道的代码,但我仍然想知道是否有内置的替代品。
代码如下:
/**
* Cast x to int, throw an exception if there's loss of information
*/
public static int safeLongToInt(long x)
{
int result = (int) x;
if (result != x)
throw new RuntimeException("long doesn't fit in an int: " + x);
return result;
}
C# 中的代码为:
int foo;
long bar = ...;
checked
{
foo = bar;
}
最佳答案
不,没有等效项,请查看此 keyword chart .
关于c# - 是否有等效于 C#'s ' checked' 关键字的 Java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1669259/