算术运算中溢出或下溢

时间:2014-05-02 14:22:30

标签: c#

我是C#的新手。我得到了这个例外 在我的计划结束时:

An unhandled exception of type 'System.ArithmeticException' occurred in mscorlib.dll

Additional information: Overflow or underflow in the arithmetic operation.

If there is a handler for this exception, the program may be safely continued.

抛出此异常的行实际上似乎与任何算术无关 操作 - 实际上我认为这是我程序的最后一行,例如,当它试图存在时。

我真的不知道出了什么问题。有没有办法可以解决这个问题?

PS。调用堆栈:

>   mscorlib.dll!double.IsPositiveInfinity(double d) + 0xc bytes    
    PresentationFramework.dll!System.Windows.Window.ValidateTopLeft(double length) + 0x25 bytes 
    PresentationFramework.dll!System.Windows.Window.CoerceTop(System.Windows.DependencyObject d, object value) + 0x64 bytes 
    WindowsBase.dll!System.Windows.DependencyObject.ProcessCoerceValue(System.Windows.DependencyProperty dp, System.Windows.PropertyMetadata metadata, ref System.Windows.EntryIndex entryIndex, ref int targetIndex, ref System.Windows.EffectiveValueEntry newEntry, ref System.Windows.EffectiveValueEntry oldEntry, ref object oldValue, object baseValue, object controlValue, System.Windows.CoerceValueCallback coerceValueCallback, bool coerceWithDeferredReference, bool coerceWithCurrentValue, bool skipBaseValueChecks) + 0x55 bytes   
    WindowsBase.dll!System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.EntryIndex entryIndex, System.Windows.DependencyProperty dp, System.Windows.PropertyMetadata metadata, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry, bool coerceWithDeferredReference, bool coerceWithCurrentValue, System.Windows.OperationType operationType) + 0x247 bytes   
    WindowsBase.dll!System.Windows.DependencyObject.CoerceValue(System.Windows.DependencyProperty dp) + 0xd5 bytes  
    PresentationFramework.dll!System.Windows.Window.SetupInitialState(double requestedTop, double requestedLeft, double requestedWidth, double requestedHeight) + 0x135 bytes   
    PresentationFramework.dll!System.Windows.Window.CreateSourceWindow(bool duringShow) + 0x30b bytes   
    PresentationFramework.dll!System.Windows.Window.CreateSourceWindowDuringShow() + 0xa bytes  
    PresentationFramework.dll!System.Windows.Window.SafeCreateWindowDuringShow() + 0x3f bytes   
    PresentationFramework.dll!System.Windows.Window.ShowHelper(object booleanBox) + 0x77 bytes  
    WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) + 0x56 bytes 
    WindowsBase.dll!MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(object source, System.Delegate method, object args, int numArgs, System.Delegate catchHandler) + 0x3a bytes    
    WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeImpl() + 0xac bytes  
    WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(object state) + 0x38 bytes 
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0xa7 bytes  
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x16 bytes  
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x41 bytes    
    WindowsBase.dll!System.Windows.Threading.DispatcherOperation.Invoke() + 0x5b bytes  
    WindowsBase.dll!System.Windows.Threading.Dispatcher.ProcessQueue() + 0x16b bytes    
    WindowsBase.dll!System.Windows.Threading.Dispatcher.WndProcHook(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) + 0x5a bytes 
    WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) + 0x9b bytes    
    WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) + 0x6b bytes    
    WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) + 0x56 bytes 
    WindowsBase.dll!MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(object source, System.Delegate method, object args, int numArgs, System.Delegate catchHandler) + 0x3a bytes    
    WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs) + 0x10e bytes 
    WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) + 0xf1 bytes 
    [Native to Managed Transition]  
    [Managed to Native Transition]  
    WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame frame) + 0xae bytes  
    WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame frame) + 0x49 bytes  
    WindowsBase.dll!System.Windows.Threading.Dispatcher.Run() + 0x4b bytes  
    PresentationFramework.dll!System.Windows.Application.RunDispatcher(object ignore) + 0x17 bytes  
    PresentationFramework.dll!System.Windows.Application.RunInternal(System.Windows.Window window) + 0x6f bytes 
    PresentationFramework.dll!System.Windows.Application.Run(System.Windows.Window window) + 0x26 bytes 
    PresentationFramework.dll!System.Windows.Application.Run() + 0x1b bytes 
    CardReaderGui.exe!CardReaderGui.App.Main() + 0x59 bytes C#
    [Native to Managed Transition]  
    [Managed to Native Transition]  
    mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x6b bytes    
    Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x27 bytes  
    mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x6f bytes   
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0xa7 bytes  
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x16 bytes  
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x41 bytes    
    mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes   
    [Native to Managed Transition]  

3 个答案:

答案 0 :(得分:8)

这是一个令人烦恼的异常",检查 Infinity 的方法如何导致溢出?当然,没有可能的浮点值大于无限。只有巴斯光年才能走向无限远。请注意,您正在编写Windows窗体应用程序,堆栈跟踪显示WPF代码,与此类问题非常相关。

您在这里处理的是硬件异常。浮点处理器引发的异常。这样的异常被CLR捕获,并作为托管异常重新引发,即System.ArithmeticException。

运行托管代码时不应该发生这种情况。但是,如果浮点处理器没有以与托管代码兼容的模式运行,则会发生 。特别是,浮点单元控制字中的异常掩码从其预期值改变。这非常可能会导致WPF应用程序出现问题,它喜欢使用NaN作为窗口坐标。

这是由您的进程中运行的非托管代码引起的。很可能是使用Borland C运行时库的代码,这是一个众所周知的麻烦制造者。您需要找到该代码并将其消除。如果您不熟悉在您的计划中使用的内容,那就不容易了。如果将这些代码注入到您的进程中,可能很难找到,例如在使用其中一个shell对话框(如OpenFileDialog)时加载的shell扩展处理程序。

启用非托管调试和Debugger + Windows +模块窗口对于获得第一个潜在客户非常重要。确保您可以考虑在该窗口中看到的每个DLL,特别注意没有Microsoft版权声明的DLL。 Sysinternals的' Process Monitor也很有帮助。祝你好运狩猎。

答案 1 :(得分:0)

除了Hans Passant非常有启发性的答案之外,我找到了一个对我有用的相对简单的解决方案。我得到了一个非常类似的错误,只发生在大约五分之一的计算机上。重新安装Visual C++ Redistributable for Visual Studio 2012 Update 4解决了这个问题。

请注意,这也适用于已安装Visual Studio的计算机。

答案 2 :(得分:0)

我知道评论这个问题太晚了,但我的情况是我在使用服务中的数据库上下文作为该服务的属性时遇到了这个错误

  public class XService : System.IDisposable
        {
         private SiactDbContext db = new SiactDbContext();
    }

 public long SaveObject (TypeX object) //In this method the error
        {
            long result = -1;
           
            try
            {
                db.TypeX.Add(object);
                db.SaveChanges();
                result =  retencion.id;
            }
            catch (Exception e)
            {
                Log.Error("Error to saving object");
                Log.Error(e.InnerException.Message);
                Log.Error(e.StackTrace);

            }
       
            return result;
        }

在方法中使用了私有的 dbContext 后,打开并处理它,它起作用了。

就这样

public long SaveObject (TypeX object)
        {
            long result = -1;
            SiactDbContext db1 = new SiactDbContext();

            try
            {
                db1.TypeX.Add(object);
                db1.SaveChanges();
                result =  object.id;
            }
            catch (Exception e)
            {
                Log.Error("Error to saving object");
                Log.Error(e.InnerException.Message);
                Log.Error(e.StackTrace);

            }
            db1.Dispose();

            return result;
        }