int y = -2147483648;
int z = unchecked(y / -1);
Run Code Online (Sandbox Code Playgroud)
第二行引起了OverflowException.不应该unchecked阻止这个?
例如:
int y = -2147483648;
int z = unchecked(y * 2);
Run Code Online (Sandbox Code Playgroud)
不会导致例外.
我无法理解这个错误:
在对方法SetVolume的调用中,Volume = 2055786000,size = 93552000.Volume是Integer属性,size也是Integer,如您所见.
该类是dbml实体类的部分类,但是此Volume属性不是数据库中的列,它仅作为"业务对象属性"存在于部分类中.
查看详情显示:
Data> Item:为了评估索引属性,必须对该属性进行限定,并且必须由用户显式提供参数.

可能导致这种情况......?
我认为这看起来像C#编译器中的一个错误.
考虑这段代码(在方法内):
const long dividend = long.MinValue;
const long divisor = -1L;
Console.WriteLine(dividend % divisor);
Run Code Online (Sandbox Code Playgroud)
它编译时没有错误(或警告).好像是一个bug.运行时,0在控制台上打印.
然后没有const,代码:
long dividend = long.MinValue;
long divisor = -1L;
Console.WriteLine(dividend % divisor);
Run Code Online (Sandbox Code Playgroud)
当它运行时,它正确地导致OverflowException被抛出.
C#语言规范专门提到了这个案例,并说System.OverflowException将抛出一个.它不依赖于上下文checked或unchecked似乎(也是与余数运算符的编译时常量操作数的错误与checked和相同unchecked).
int(System.Int32),而不仅仅是long(System.Int64)发生同样的错误.
相比之下,编译器处理dividend / divisor与const操作数比要好得多dividend % divisor.
我的问题:
我是对的,这是一个错误吗?如果是,它是一个众所周知的错误,他们不希望修复(因为向后兼容性,即使使用% -1编译时常量相当愚蠢-1)?或者我们应该报告它,以便他们可以在即将推出的C#编译器版本中修复它?
我希望能够将高值无符号整数(使用最高位的值)转换为有符号整数.在这种情况下,我不关心该值是否高于有符号整数类型的最大值.我只是希望它转换为位值表示的有符号整数.换句话说,我希望它会导致负数.
但是,使用VB.NET,CType操作不会以这种方式工作(或任何其他转换函数,如CShort和CInteger).当您尝试转换高于所需签名类型的最大值的无符号值时,它会抛出OverflowException而不是返回负数.例如:
Dim x As UShort = UShort.MaxValue
Dim y As Short = CShort(x) ' Throws OverflowException
Run Code Online (Sandbox Code Playgroud)
值得一提的是,该DirectCast操作不能用于在有符号和无符号类型之间转换值,因为这两种类型都不继承或实现另一种类型.例如:
Dim x As UShort = UShort.MaxValue
Dim y As Short = DirectCast(x, Short) ' Won't compile: "Value of type 'UShort' cannot be converted to 'Short'
Run Code Online (Sandbox Code Playgroud)
我找到了一种方法来做我想做的事,但它看起来不必要的丑陋.以下是我如何使用它:
Dim x As UShort = UShort.MaxValue
Dim y As Short = BitConverter.ToInt16(BitConverter.GetBytes(x), 0) ' y gets set to -1
Run Code Online (Sandbox Code Playgroud)
就像我说的那样,但是如果在VB.NET中有一种更简单,更清晰的方式,我很想知道它是什么.
我正在使用
Decimal.Round(decimal d)
Run Code Online (Sandbox Code Playgroud)
MSDN说它可以抛出OverflowException
https://msdn.microsoft.com/en-us/library/k4e2bye2(v=vs.110).aspx
我不确定如何发生这种情况.我尝试使用ilSpy查看实现并获得直到外部实现:
// decimal
[SecurityCritical]
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void FCallRound(ref decimal d, int decimals);
Run Code Online (Sandbox Code Playgroud)
有没有人知道什么输入可以抛出这个异常?
我知道在互联网上有几个关于这个问题的主题和帖子,我已经阅读过它们(不是每篇文章,我都要承认),但没有一个完全满足我.
我的情况:
我正在使用ODP.net(dll版本2.111.6.0)访问Oracle DB(版本10 + 11)和DataReader来检索数据(.NET 3.5,C#).
使用此代码会导致' System.OverflowException(算术运算导致溢出).
decimal.TryParse(oraReader.GetOracleDecimal(0).Value.ToString(),
NumberStyles.Any, null, out parsedOraDecimal)
Run Code Online (Sandbox Code Playgroud)
这一个导致值为' 3,0000000000000000000000000000000000000000000000000000000000000E-126 '
decimal.TryParse(oraReader.GetOracleValue(0).ToString(),
NumberStyles.Any, null, out parsedOraDecimal)
Run Code Online (Sandbox Code Playgroud)
现在我必须找到一些方法来正确检索和评估这个值 - 数据库也可以从我无法控制的其他应用程序中使用,因此无法进行更改.
将我的C#代码中的类型从'decimal'转换为'double'也不是一个真正的选择.
有任何想法吗?
OverflowError在经过一些愚蠢的计算之后捕获Python ,我检查了错误args并看到它是一个包含整数作为其第一个坐标的元组.我假设这是某种错误号(errno).但是,我找不到任何文档或参考资料.
例:
try:
1e4**100
except OverflowError as ofe:
print ofe.args
## prints '(34, 'Numerical result out of range')'
Run Code Online (Sandbox Code Playgroud)
你知道34在这种情况下意味着什么吗?你知道这个例外的其他可能的错误号吗?
在我的c#dll中,我有一些像这样的代码与一些非托管的dll进行交互:
IntPtr buffer = ...;
TTPOLYGONHEADER header = (TTPOLYGONHEADER)Marshal.PtrToStructure(
new IntPtr(buffer.ToInt32() + index), typeof(TTPOLYGONHEADER));
Run Code Online (Sandbox Code Playgroud)
在安装Windows 8之前,在x64系统上使用AnyCPU中的.Net2和.Net4编译的dll时,这一直都能正常工作.
在使用.Net4 dll的Windows 8中,我在buffer.ToInt32()调用时得到OverFlowException("算术操作导致溢出.").
IntPtr.ToInt32()的MSDN文档说明了这一点:
"OverflowException:在64位平台上,此实例的值太大或太小,无法表示为32位有符号整数."
我想知道为什么这个问题只出现在Windows 8上,以及解决它的正确方法是什么.
我应该使用这样的方法,而不是IntPtr.ToInt32()调用吗?
internal static long GetPtr(IntPtr ptr)
{
if (IntPtr.Size == 4) // x86
return ptr.ToInt32();
return ptr.ToInt64(); // x64
}
Run Code Online (Sandbox Code Playgroud) 编写完可以归结为以下内容的代码后:
var size=-1;
var arr=new byte[size];
Run Code Online (Sandbox Code Playgroud)
我很惊讶它抛出了一个OverflowException.OverflowException状态的文档:
我无法看到为这个异常提供的描述如何提供负大小和数组长度,所以深入研究并发现这确实是指定的行为:
尺寸长度的计算值验证如下.如果一个或多个值小于零,则抛出System.OverflowException,并且不执行进一步的步骤.
我想知道为什么选择了OverflowException.如果你问我,那会很误导.我花了至少5分钟的调查时间(不计算我的思考).任何人都可以对这个(我认为)特殊的设计决定有所了解吗?
我正在编写一个应用程序,并希望使用该TaskDialogIndirect函数 - 但是我不想写大量的P/Invoke等等,所以我已经包含了WinAPICodePack.但是有一个问题!当我为TaskDialog它创建一个控件并将其添加到对话框时,一切正常.但是,运行Show方法会导致OverflowExceptionmscorlib.我将其跟踪到一些指针处理和编组.
尝试使用代码得到了结果,我发现,必须为.NET 3.5和包含应用程序TOO编译DLL .在.NET 4.0上安装我的应用程序会调用此错误...有关解决方法的任何想法 - 或者您甚至可以重现此错误?
另一个问题是我可以将Icon属性设置为我想要的,但图标根本不显示.我必须设置一个Opened动态设置图标的事件......
信息:在Windows 8 Pro x64 RTM上运行Visual Studio 2012 RTM.应用程序是标准的WPF应用程序.
示例代码:
TaskDialog td = new TaskDialog();
td.Cancelable = true;
td.Caption = "Caption";
td.InstructionText = "Instructions";
td.Text = "Text";
TaskDialogCommandLink buttonElevation =
new TaskDialogCommandLink("elevation", "Elevation Required Sample");
buttonElevation.UseElevationIcon = true;
td.Controls.Add(buttonElevation);
td.Show(); // OverflowException occurs here!
Run Code Online (Sandbox Code Playgroud) .net winapi marshalling overflowexception windows-api-code-pack