System overflowexception ошибка переполнения

«Исключение, которое выдается, если выполнение арифметической операции, операции приведения к типу или преобразования в проверяемом контексте приводит к переполнению.» На Msdn

Convert.ToInt16(r[3]) — ошибка скорее всего тут.

Если r[3] является объектом, который имеет тип Int32, то можете использовать следующий метод явного преобразования: (Int16)r[3].

В настройках проекта на вкладке Build жмём кнопку Advanced и снимаем галочку Check for arithmetic overflow, тогда ошибки переполнения не возникнет.

Важно понимать, что при таком переполнении в этом примере (Int16)r[3] вы получите неожиданный результат.

Если r[3] является строкой, то попробуйте воспользоваться конструкцией: try {} catch {}

Aug 19, 2017 5:39:12 PM |
.NET Exceptions — System.OverflowException

A look at the System.OverflowException in .NET, including C# code showing how overflow manipulations and rollovers are handled.

Moving along through our detailed .NET Exception Handling series, today we’ll be tackling the ever-popular System.OverflowException. With a name that most developers will probably recognize, the System.OverflowException in .NET indicates that an invalid arithmetic, casting, or conversion error occurred within a checked context.

Throughout this article we’ll explore the System.OverflowException in greater detail, starting with a look at the (rather simple) .NET exception hierarchy chain into which it falls. We’ll also go over a few functional C# code samples that will illustrate the two different scenarios in which System.OverflowExceptions are commonly thrown, so you’re better able to handle them in your own code. Let’s get going!

The Technical Rundown

  • All .NET exceptions are derived classes of the System.Exception base class, or derived from another inherited class therein.
  • System.SystemException is inherited from the System.Exception class.
  • System.OverflowException inherits from System.SystemException.

Full Code Sample

Below is the full code sample we’ll be using in this article. It can be copied and pasted if you’d like to play with the code yourself and see how everything works.

// <Airbrake.OverflowException>/Program.cs
using System;
using System.Reflection;
using Utility;

namespace Airbrake.OverflowException
{
class Program
{
static void Main(string[] args)
{
// Multiple one billion by three in checked context.
Logging.LineSeparator("CHECKED MULTIPLICATION");
MultiplyNumbers(1_000_000_000, 3);

// Multiple one billion by three in unchecked context.
Logging.LineSeparator("UNCHECKED MULTIPLICATION");
MultiplyNumbers(1_000_000_000, 3, false);

const int value = 200;
// Convert 200 to SByte in checked context.
Logging.LineSeparator("CHECKED TYPE CONVERSION");
ConvertValueToType<sbyte>(value);

// Convert 200 to SByte in unchecked context.
Logging.LineSeparator("UNCHECKED TYPE CONVERSION");
ConvertValueToType<sbyte>(value, false);

// Convert 200 directly, without Convert.ChangeType().
Logging.LineSeparator("DIRECT UNCHECKED TYPE CONVERSION");
unchecked
{
var result = (sbyte) value;
// Output result.
Logging.Log($"[UNCHECKED] {value:n0} converted to type {result.GetType().Name}: {result:n0}.");
}
}

/// <summary>
/// Attempts to multiple two passed integer values together.
/// </summary>
/// <param name="a">First value.</param>
/// <param name="b">Second value.</param>
/// <param name="checked">Determines if 'checked' context is used for multiplication attempt.</param>
internal static void MultiplyNumbers(int a, int b, bool @checked = true)
{
try
{
int result;
if (@checked)
{
// Enable overflow checking.
checked
{
// Multiple numbers.
result = a * b;
}
}
// Disable overflow checking.
unchecked
{
// Multiple numbers.
result = a * b;
}
// Output result.
Logging.Log($"[{(@checked ? "CHECKED" : "UNCHECKED")}] {a:n0} * {b:n0} = {result:n0}");
}
catch (System.OverflowException exception)
{
// Catch expected OverflowExceptions.
Logging.Log(exception);
Logging.Log($"[{(@checked ? "CHECKED" : "UNCHECKED")}] {a:n0} * {b:n0} exceeds int.MaxValue: {int.MaxValue:n0}");
}
catch (Exception exception)
{
// Catch unexpected Exceptions.
Logging.Log(exception, false);
}
}

/// <summary>
/// Attempts to convert passed value to specified generic type T.
/// </summary>
/// <typeparam name="T">Generic type to convert to.</typeparam>
/// <param name="value">Value to be converted.</param>
/// <param name="checked">Determines if 'checked' context is used for multiplication attempt.</param>
internal static void ConvertValueToType<T>(int value, bool @checked = true)
{
try
{
object result;
if (@checked)
{
// Enable overflow checking.
checked
{
// Convert to type T.
result = (T) Convert.ChangeType(value, typeof(T));

}
}
// Disable overflow checking.
unchecked
{
// Convert to type T.
result = (T) Convert.ChangeType(value, typeof(T));
}
// Output result.
Logging.Log($"[{(@checked ? "CHECKED" : "UNCHECKED")}] {value:n0} converted to type {result.GetType().Name}: {result:n0}.");
}
catch (System.OverflowException exception)
{
// Catch expected OverflowExceptions.
Logging.Log(exception);
// Since this is a generic type, need to use reflection to get the MaxValue field, if applicable.
var maxValueField = typeof(T).GetField("MaxValue", BindingFlags.Public
| BindingFlags.Static);
if (maxValueField == null)
{
throw new NotSupportedException(typeof(T).Name);
}
var maxValue = (T) maxValueField.GetValue(null);

Logging.Log($"[{(@checked ? "CHECKED" : "UNCHECKED")}] {value:n0} cannot be converted to type {typeof(T).Name} because it exceeds {typeof(T).Name}.MaxValue: {maxValue:n0}");
}
catch (Exception exception)
{
// Catch unexpected Exceptions.
Logging.Log(exception, false);
}
}
}
}

// <Utility>/Logging.cs
using System;
using System.Diagnostics;

namespace Utility
{
/// <summary>
/// Houses all logging methods for various debug outputs.
/// </summary>
public static class Logging
{
private const char SeparatorCharacterDefault = '-';
private const int SeparatorLengthDefault = 40;

/// <summary>
/// Determines type of output to be generated.
/// </summary>
public enum OutputType
{
/// <summary>
/// Default output.
/// </summary>
Default,
/// <summary>
/// Output includes timestamp prefix.
/// </summary>
Timestamp
}

/// <summary>
/// Outputs to <see cref="Debug.WriteLine(String)"/>.
/// </summary>
/// <param name="value">Value to be output to log.</param>
/// <param name="outputType">Output type.</param>
public static void Log(string value, OutputType outputType = OutputType.Default)
{
Debug.WriteLine(outputType == OutputType.Timestamp
? $"[{StopwatchProxy.Instance.Stopwatch.Elapsed}] {value}"
: value);
}

/// <summary>
/// When <see cref="Exception"/> parameter is passed, modifies the output to indicate
/// if <see cref="Exception"/> was expected, based on passed in `expected` parameter.
/// <para>Outputs the full <see cref="Exception"/> type and message.</para>
/// </summary>
/// <param name="exception">The <see cref="Exception"/> to output.</param>
/// <param name="expected">Boolean indicating if <see cref="Exception"/> was expected.</param>
/// <param name="outputType">Output type.</param>
public static void Log(Exception exception, bool expected = true, OutputType outputType = OutputType.Default)
{
var value = $"[{(expected ? "EXPECTED" : "UNEXPECTED")}] {exception}: {exception.Message}";

Debug.WriteLine(outputType == OutputType.Timestamp
? $"[{StopwatchProxy.Instance.Stopwatch.Elapsed}] {value}"
: value);
}

/// <summary>
/// Outputs to <see cref="Debug.WriteLine(Object)"/>.
///
/// ObjectDumper: http://stackoverflow.com/questions/852181/c-printing-all-properties-of-an-object&amp;lt;/cref
/// </summary>
/// <param name="value">Value to be output to log.</param>
/// <param name="outputType">Output type.</param>
public static void Log(object value, OutputType outputType = OutputType.Default)
{
Debug.WriteLine(outputType == OutputType.Timestamp
? $"[{StopwatchProxy.Instance.Stopwatch.Elapsed}] {ObjectDumper.Dump(value)}"
: ObjectDumper.Dump(value));
}

/// <summary>
/// Outputs a dashed line separator to <see cref="Debug.WriteLine(String)"/>.
/// </summary>
/// <param name="length">Total separator length.</param>
/// <param name="char">Separator character.</param>
public static void LineSeparator(int length = SeparatorLengthDefault, char @char = SeparatorCharacterDefault)
{
Debug.WriteLine(new string(@char, length));
}

/// <summary>
/// Outputs a dashed line separator to <see cref="Debug.WriteLine(String)"/>,
/// with inserted text centered in the middle.
/// </summary>
/// <param name="insert">Inserted text to be centered.</param>
/// <param name="length">Total separator length.</param>
/// <param name="char">Separator character.</param>
public static void LineSeparator(string insert, int length = SeparatorLengthDefault, char @char = SeparatorCharacterDefault)
{
// Default output to insert.
var output = insert;

if (insert.Length < length)
{
// Update length based on insert length, less a space for margin.
length -= insert.Length + 2;
// Halve the length and floor left side.
var left = (int) Math.Floor((decimal) (length / 2));
var right = left;
// If odd number, add dropped remainder to right side.
if (length % 2 != 0) right += 1;

// Surround insert with separators.
output = $"{new string(@char, left)} {insert} {new string(@char, right)}";
}

// Output.
Debug.WriteLine(output);
}
}
}

When Should You Use It?

To understand what the System.OverflowException is, we should first briefly examine what overflow errors are in a more general sense. There are a few different types of overflow in computing.

As you may recall, we explored the notion of stack overflow in our .NET Exception Handling – System.StackOverflowException article. Feel free to check that out for all the details, but the long and short of it is that a stack overflow occurs when an application attempts to utilize more memory than was allocated to it in the memory address space. This results in a stack overflow exception in most applications, because the system recognizes the application is attempting to use memory that doesn’t «belong» to it.

As in the case of the System.OverflowException, a general overflow typically refers to an integer overflow. This is similar to a stack overflow, except that, instead of attempting to use memory outside of the allowable bounds, the application is attempting to create numeric values outside the allowable bounds. We explored this concept a bit in our Ruby Exception Handling: NoMemoryError article. The basic issue is that any given data type, such as an integer, has a maximum value based on the number of bits (or bytes) it is allocated. When your application attempts to create a value that would exceed that number of bits to store, unless it can be automatically converted to a larger data type, an integer overflow is thrown.

To see this in action we’ve created two methods for testing. The first of these methods is MultiplyNumbers(int a, int b, bool @checked = true):

/// <summary>
/// Attempts to multiple two passed integer values together.
/// </summary>
/// <param name="a">First value.</param>
/// <param name="b">Second value.</param>
/// <param name="checked">Determines if 'checked' context is used for multiplication attempt.</param>
internal static void MultiplyNumbers(int a, int b, bool @checked = true)
{
try
{
int result;
if (@checked)
{
// Enable overflow checking.
checked
{
// Multiple numbers.
result = a * b;
}
}
// Disable overflow checking.
unchecked
{
// Multiple numbers.
result = a * b;
}
// Output result.
Logging.Log($"[{(@checked ? "CHECKED" : "UNCHECKED")}] {a:n0} * {b:n0} = {result:n0}");
}
catch (System.OverflowException exception)
{
// Catch expected OverflowExceptions.
Logging.Log(exception);
Logging.Log($"[{(@checked ? "CHECKED" : "UNCHECKED")}] {a:n0} * {b:n0} exceeds int.MaxValue: {int.MaxValue:n0}");
}
catch (Exception exception)
{
// Catch unexpected Exceptions.
Logging.Log(exception, false);
}
}

Nothing too fancy going on here, except we’re using the @checked boolean parameter to determine if we should use a checked or unchecked context during the calculation. In C#, a checked context forces the common language runtime (CLR) to raise System.OverflowExceptions when overflows occur, while unchecked context simply ignores them and truncates the resulting value.

To test out our MultiplyNumbers(int a, int b, bool @checked = true) method we are making two calls to it:

// Multiple one billion by three in checked context.
Logging.LineSeparator("CHECKED MULTIPLICATION");
MultiplyNumbers(1_000_000_000, 3);

// Multiple one billion by three in unchecked context.
Logging.LineSeparator("UNCHECKED MULTIPLICATION");
MultiplyNumbers(1_000_000_000, 3, false);

If you aren’t familiar, the underscores (_) in our large number literal are a new feature added in C# 7.0 called digit separators, which allow us to visually change how numbers are displayed without impacting their underlying value. Feel free to read more about this and other C# 7.0 features in our What’s New in C# 7.0? – Digit Separators, Reference Returns, and Binary Literals article.

Anyway, as you can see we’re merely attempting to multiple one billion by three, which should result in a value of three billion. Executing these calls gives us the following output:

-------- CHECKED MULTIPLICATION --------
[EXPECTED] System.OverflowException: Arithmetic operation resulted in an overflow.
[CHECKED] 1,000,000,000 * 3 exceeds int.MaxValue: 2,147,483,647
------- UNCHECKED MULTIPLICATION -------
[UNCHECKED] 1,000,000,000 * 3 = -1,294,967,296

As we can see, the first checked context call throws a System.OverflowException because the total value of three billion exceeds the maximum value that can be stored in an integer of 2,147,483,647. On the other hand, the second unchecked context call doesn’t throw an error, and instead produces an interesting result of -1,294,967,296. Why on earth are we getting a negative result when multiplying two positive numbers together? The answer lies in how the computer handles overflow truncation.

To examine this, what what happens if we take the -1,294,967,296 result we got, then subtract int.MinValue and add int.MaxValue to it:

-1,294,967,296 - -2,147,483,648 + 2,147,483,647 = 2,999,999,999

That’s incredibly close to our expected result of three billion. In fact, the loss of one extra digit is due to the fact that signed number values must represent the number zero with a particular combination of bits. This «extra» value that is taken up during overflow rollover/truncation is lost in our resulting number above, giving us one less than the expected value when added back together.

Cool. Now let’s take a look at the ConvertValueToType<T>(int value, bool @checked = true) method:

/// <summary>
/// Attempts to convert passed value to specified generic type T.
/// </summary>
/// <typeparam name="T">Generic type to convert to.</typeparam>
/// <param name="value">Value to be converted.</param>
/// <param name="checked">Determines if 'checked' context is used for multiplication attempt.</param>
internal static void ConvertValueToType<T>(int value, bool @checked = true)
{
try
{
object result;
if (@checked)
{
// Enable overflow checking.
checked
{
// Convert to type T.
result = (T) Convert.ChangeType(value, typeof(T));

}
}
// Disable overflow checking.
unchecked
{
// Convert to type T.
result = (T) Convert.ChangeType(value, typeof(T));
}
// Output result.
Logging.Log($"[{(@checked ? "CHECKED" : "UNCHECKED")}] {value:n0} converted to type {result.GetType().Name}: {result:n0}.");
}
catch (System.OverflowException exception)
{
// Catch expected OverflowExceptions.
Logging.Log(exception);
// Since this is a generic type, need to use reflection to get the MaxValue field, if applicable.
var maxValueField = typeof(T).GetField("MaxValue", BindingFlags.Public
| BindingFlags.Static);
if (maxValueField == null)
{
throw new NotSupportedException(typeof(T).Name);
}
var maxValue = (T) maxValueField.GetValue(null);

Logging.Log($"[{(@checked ? "CHECKED" : "UNCHECKED")}] {value:n0} cannot be converted to type {typeof(T).Name} because it exceeds {typeof(T).Name}.MaxValue: {maxValue:n0}");
}
catch (Exception exception)
{
// Catch unexpected Exceptions.
Logging.Log(exception, false);
}
}

This one is a bit more complex because we’re allowing the method to be called with a generic type (T). This method attempts to convert the passed value into whatever type T was provided. We can use this to make some type conversions in our calling code:

const int value = 200;
// Convert 200 to SByte in checked context.
Logging.LineSeparator("CHECKED TYPE CONVERSION");
ConvertValueToType<sbyte>(value);

// Convert 200 to SByte in unchecked context.
Logging.LineSeparator("UNCHECKED TYPE CONVERSION");
ConvertValueToType<sbyte>(value, false);

Just as before, we’re making two calls to our method, the first being checked and the second unchecked context. Our goal here is to convert the int value of 200 into a sbyte, which is stored as a signed 8-bit integer in the background. Calling this code produces the following output:

------- CHECKED TYPE CONVERSION --------
[EXPECTED] System.OverflowException: Value was either too large or too small for a signed byte.
[CHECKED] 200 cannot be converted to type SByte because it exceeds SByte.MaxValue: 127
------ UNCHECKED TYPE CONVERSION -------
[EXPECTED] System.OverflowException: Value was either too large or too small for a signed byte.
at System.Convert.ToSByte(Int32 value)
at System.Int32.System.IConvertible.ToSByte(IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType)
at Airbrake.OverflowException.Program.ConvertValueToType[T](Int32 value, Boolean checked) in D:\work\Airbrake.io\Exceptions\.NET\Airbrake.OverflowException\Program.cs:line 106: Value was either too large or too small for a signed byte.
[UNCHECKED] 200 cannot be converted to type SByte because it exceeds SByte.MaxValue: 127

The first checked context call, as expected, throws a System.OverflowException because we’re attempting to convert a value of 200 into a signed 8-bit integer, which can only handle a maximum positive value of 127. However, our unchecked context call is also throwing a System.OverflowException for the same reason, even though it shouldn’t produce overflow errors. The reason can be gleaned from the stack trace that produced the error, which I’ve left in the output above. Since our method makes use of the Convert.ChangeType() method to perform the actual conversion, that built-in .NET method is overriding our unchecked context setting and enabling a checked context within its own execution.

To remedy this we can try explicitly converting our int value to an sbyte:

// Convert 200 directly, without Convert.ChangeType().
Logging.LineSeparator("DIRECT UNCHECKED TYPE CONVERSION");
unchecked
{
var result = (sbyte) value;
// Output result.
Logging.Log($"[UNCHECKED] {value:n0} converted to type {result.GetType().Name}: {result:n0}.");
}

Sure enough, running this code results in a properly converted value, as expected:

--- DIRECT UNCHECKED TYPE CONVERSION ---
[UNCHECKED] 200 converted to type SByte: -56.

As before, the result is negative because we’ve «rolled over» the maximum positive value of 127, just as we did above with our integer multiplication attempts.

To get the most out of your own applications and to fully manage any and all .NET Exceptions, check out the Airbrake .NET Bug Handler, offering real-time alerts and instantaneous insight into what went wrong with your .NET code, along with built-in support for a variety of popular development integrations including: JIRA, GitHub, Bitbucket, and much more.

0 / 0 / 0

Регистрация: 12.01.2020

Сообщений: 2

1

.NET 4.x

13.01.2020, 00:07. Показов 2157. Ответов 1


Студворк — интернет-сервис помощи студентам

Возникает такая ошибка при клике на форме(в некоторых точках):

Кликните здесь для просмотра всего текста

Ошибка переполнения при DrawImage

Код:

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public partial class Form1 : Form
    {
        public RectangleF Player;
        public PointF PlayerPos;
        public PointF target;
        public PointF GoTo;
 
        public Brush brush;
 
        public Form1()
        {
            SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint |
                     ControlStyles.OptimizedDoubleBuffer, true);
 
            PlayerPos = new PointF(400, 400);
            Player = new RectangleF(PlayerPos.X, PlayerPos.Y, 40, 40);
            brush = Brushes.Aqua;
 
            InitializeComponent();
        }
 
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
 
            g.DrawImage(TestImg.Test, Player.Location);
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            Player.X += GoTo.X * 2;
            Player.Y += GoTo.Y * 2;
 
            Refresh();
        }
 
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            target = e.Location;
 
            PointF direction = new PointF(target.X - Player.X, target.Y - Player.Y);
            float length = (float)Math.Sqrt(direction.X * direction.X + direction.X * direction.Y);
            direction.X = (float)Math.Round(direction.X / length, MidpointRounding.ToEven);
            direction.Y = (float)Math.Round(direction.Y / length, MidpointRounding.ToEven);
 
            GoTo.X = direction.X;
            GoTo.Y = direction.Y;
        }
    }

Подробности исключения:

Кликните здесь для просмотра всего текста

System.OverflowException
HResult=0x80131516
Сообщение = Ошибка переполнения.
Источник = System.Drawing
Трассировка стека:
в System.Drawing.Graphics.CheckErrorStatus(Int32 status)
в System.Drawing.Graphics.DrawImage(Image image, Single x, Single y)
в System.Drawing.Graphics.DrawImage(Image image, PointF point)
в Test.Form1.Form1_Paint(Object sender, PaintEventArgs e) в C:\C#Уроки\моиЗадания\RPG\Test\Form1.cs:строка 38
в System.Windows.Forms.Control.OnPaint(PaintEventArgs e)
в System.Windows.Forms.Form.OnPaint(PaintEventArgs e)
в System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
в System.Windows.Forms.Control.WmPaint(Message& m)
в System.Windows.Forms.Control.WndProc(Message& m)
в System.Windows.Forms.ScrollableControl.WndProc(Message& m)
в System.Windows.Forms.Form.WndProc(Message& m)
в System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
в System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
в System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Помогите пожалуйста понять в чем проблема.



0



The exception that is thrown when an arithmetic, casting, or conversion operation in a checked context results in an overflow.

Minimum version: >= 1.1 >= Core 1.0

Statistics

elmah.io logo
19

How to handle it

  • Try / Catch
  • Conditional catch
  • Log but don’t catch
try
{

}
catch (System.OverflowException e)
{

}

try
{

}
catch (System.OverflowException e) when (e.Message.Contains("something"))
{

}

try
{

}
catch (System.OverflowException e) when (LogException(e))
{

}

private static bool LogException(Exception e)
{
    logger.LogError(...);
    return false;
}

How to avoid it

We haven’t written anything about avoiding this exception yet. Got a good tip on how to avoid throwing System.OverflowException? Feel free to reach out through the support widget in the lower right corner with your suggestions.

Links

  • OverflowException Class (System) | Microsoft Docs

YouTube videos

Possible fixes from StackOverflow

  • LukeH

    LukeH

    10/15/2010 4:48:30 PM

  • Joey

  • Hans Passant

    Hans Passant

    5/18/2017 3:09:35 PM

  • haim770

    haim770

    6/16/2013 7:51:20 AM

    [Browsable(false)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    [Obsolete("Property '" + nameof(Duration) + "' should be used instead.")]        
    public long DurationTicks { get; set; }

    [NotMapped]
    public TimeSpan Duration
    {
#pragma warning disable 618
      get { return new TimeSpan(DurationTicks); }
      set { DurationTicks = value.Ticks; }
#pragma warning restore 618
    }

Update

This is now achievable since EF Core 2.1, using Value Conversion.

builder.Entity<Stage>()
    .Property(s => s.Span)
    .HasConversion(new TimeSpanToTicksConverter()); // or TimeSpanToStringConverter

Because the specification says so in section 7.6.10.4:

Each expression in the expression list must be of type int, uint, long, or ulong, or implicitly convertible to one or more of these types.

This is most likely to easily allow creation of arrays larger than 2 GiB, even though they are not supported yet (but will be without a language change once the CLR makes such a change). Mono does support this, however and .NET 4.5 apparently will allow larger arrays too.

Regarding array length being an int by the way: There is also LongLength, returning a long. This was in .NET 1.1 and probably a future-proofing change.

This corner-case is very specifically addressed in the compiler. Most relevant comments and code in the Roslyn source:

// Although remainder and division always overflow at runtime with arguments int.MinValue/long.MinValue and -1     
// (regardless of checked context) the constant folding behavior is different.     
// Remainder never overflows at compile time while division does.    
newValue = FoldNeverOverflowBinaryOperators(kind, valueLeft, valueRight);

And:

// MinValue % -1 always overflows at runtime but never at compile time    
case BinaryOperatorKind.IntRemainder:
    return (valueRight.Int32Value != -1) ? valueLeft.Int32Value % valueRight.Int32Value : 0;
case BinaryOperatorKind.LongRemainder:
    return (valueRight.Int64Value != -1) ? valueLeft.Int64Value % valueRight.Int64Value : 0;

Also the behavior of the legacy C++ version of compiler, going all the way back to version 1. From the SSCLI v1.0 distribution, clr/src/csharp/sccomp/fncbind.cpp source file:

case EK_MOD:
    // if we don't check this, then 0x80000000 % -1 will cause an exception...
    if (d2 == -1) {
        result = 0;
    } else {
        result = d1 % d2;
    }
    break;

So conclusion to draw that this was not overlooked or forgotten about, at least by the programmers that worked on the compiler, it could perhaps be qualified as insufficiently precise language in the C# language specification. More about the runtime trouble caused by this killer poke in this post.

In this line:

Span = new TimeSpan(1, 0, 0, 0)

You’re using this constructor:

public TimeSpan(int days, int hours, int minutes, int seconds);

So you’re actually creating a TimeSpan greater than 24 hours since you’re passing 1 to the days parameter, while your underlying Database type is Time which only accepts values between 00:00-23:59.

Hard to tell whether you actually meant to have a TimeSpan with 1 day, or it’s just a typo.

If you really want a TimeSpan greater than 24 hours, i guess you’ll have to map your field to another Database type (like SmallDateTime).

If it’s just a typo error, just change your line to:

Span = new TimeSpan(1, 0, 0),

Source: Stack Overflow

Я недавно сталкивался с OverflowException в GDI + с помощью стороннего инструмента, который вызывает у меня несколько проблем. Я пытаюсь выяснить причину этого, а затем решить, есть ли какое-либо решение проблемы.

Ошибка происходит в SafeNativeMethods.Gdip.GdipFillPath(), возвращая код ошибки 11, который равен GpStatus.ValueOverflow. Я понятия не имею, что это значит или что может быть причиной этого. Последствия довольно велики, потому что у меня большая площадь экрана, которую нельзя рисовать.

Трассировка стека для этой ошибки:

Ошибка System.OverflowException: ошибка переполнения.  в System.Drawing.Graphics.CheckErrorStatus(статус Int32)  в System.Drawing.Graphics.FillPath(кисть кисти, путь GraphicsPath)  в Northwoods.GoShape.DrawPath(графика g, представление GoView, ручка Pen, кисть кисти, путь GraphicsPath)  в Northwoods.GoRoundedRectangle.Paint(Графика g, представление GoView)  в Northwoods.GoLayer.Paint(Графика g, представление GoView, RectangleF clipRect)  в Northwoods.GoView.PaintObjects(Boolean doc, Boolean view, Graphics g, RectangleF clipRect)  в Northwoods.GoView.PaintView(Graphics g, RectangleF clipRect)  в Northwoods.GoView.onPaintCanvas(PaintEventArgs evt)  в Northwoods.GoView.OnPaint(PaintEventArgs evt)  в System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16-слой, Boolean disposeEventArgs)  в System.Windows.Forms.Control.WmPaint(Message & m)  в System.Windows.Forms.Control.WndProc(Message & m)  в System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message & m)  в System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message & m)  в System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Существует, очевидно, 2 параметра, которые используются здесь. Графика, которую я не контролирую, и кисть. Кисть возвращается статическим методом, и чтобы быть уверенным, что это не проблема с ее Disposed, я попытался изменить Brush propety, чтобы вернуть клон, как показано ниже. Однако это, похоже, не повлияло.

public Brush 
{
   get {
         return MappingLinearGradient(this.Bounds).Clone();
       }
}

Понравилась статья? Поделить с друзьями:
  • Swtor русификатор ошибка необрабатываемое исключение
  • System fault service now touareg ошибка
  • Swtor ошибка подключения
  • Sw19 ru коды ошибок
  • Swshellfilelauncher возникла ошибка