OverflowException クラス

定義

checked コンテキストの算術演算、キャスト演算、または変換演算でオーバーフローが発生するとスローされる例外。

public ref class OverflowException : ArithmeticException
public class OverflowException : ArithmeticException
[System.Serializable]
public class OverflowException : ArithmeticException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class OverflowException : ArithmeticException
type OverflowException = class
    inherit ArithmeticException
[<System.Serializable>]
type OverflowException = class
    inherit ArithmeticException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type OverflowException = class
    inherit ArithmeticException
Public Class OverflowException
Inherits ArithmeticException
継承
継承
属性

注釈

OverflowExceptionは、実行時に次の条件下でスローされます。

  • 算術演算では、演算によって返されるデータ型の範囲外の結果が生成されます。 次の例は、型の境界を OverflowException オーバーフローする乗算演算によってスローされる を Int32 示しています。

    int value = 780000000;
    checked {
    try {
       // Square the original value.
       int square = value * value;
       Console.WriteLine("{0} ^ 2 = {1}", value, square);
    }
    catch (OverflowException) {
       double square = Math.Pow(value, 2);
       Console.WriteLine("Exception: {0} > {1:E}.",
                         square, Int32.MaxValue);
    } }
    // The example displays the following output:
    //       Exception: 6.084E+17 > 2.147484E+009.
    
    open Checked
    
    let v = 780000000
    try
       // Square the original value.
       let square = v * v
       printfn $"{v} ^ 2 = {square}"
    with :? OverflowException ->
        let square = float v ** 2
        printfn $"Exception: {square} > {Int32.MaxValue:E}."
    // The example displays the following output:
    //       Exception: 6.084E+17 > 2.147484E+009.
    
    Dim value As Integer = 780000000
    Try
       ' Square the original value.
       Dim square As Integer = value * value 
       Console.WriteLine("{0} ^ 2 = {1}", value, square)
    Catch e As OverflowException
       Dim square As Double = Math.Pow(value, 2)
       Console.WriteLine("Exception: {0} > {1:E}.", _
                         square, Int32.MaxValue)
    End Try
    ' The example displays the following output:
    '       Exception: 6.084E+17 > 2.147484E+009.
    
  • キャストまたは変換操作は縮小変換を実行しようとします。ソース データ型の値がターゲット データ型の範囲外です。 次の例は、大きな符号なしバイト値を符号付きバイト値に変換しようとした場合にスローされる を示しています OverflowException

    byte value = 241;
    checked {
    try {
       sbyte newValue = (sbyte) value;
       Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                         value.GetType().Name, value,
                         newValue.GetType().Name, newValue);
    }
    catch (OverflowException) {
       Console.WriteLine("Exception: {0} > {1}.", value, SByte.MaxValue);
    } }
    // The example displays the following output:
    //       Exception: 241 > 127.
    
    open Checked
    
    let value = 241uy
    try
        let newValue = int8 value
        printfn $"Converted the {value.GetType().Name} value {value} to the {newValue.GetType().Name} value {newValue}."
    with :? OverflowException ->
        printfn $"Exception: {value} > {SByte.MaxValue}."
    // The example displays the following output:
    //       Exception: 241 > 127.
    
    Dim value As Byte = 241
    Try
       Dim newValue As SByte = (CSByte(value))
       Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", _
                         value.GetType().Name, value, _
                         newValue.GetType().Name, newValue)
    Catch e As OverflowException
       Console.WriteLine("Exception: {0} > {1}.", value, SByte.MaxValue)
    End Try                            
    ' The example displays the following output:
    '       Exception: 241 > 127.
    

いずれの場合も、操作の結果は、プロパティより小さいか、操作の MinValue 結果のデータ型のプロパティより MaxValue 大きい値になります。

算術演算、キャスト操作、または変換操作で を OverflowExceptionスローするには、チェックされたコンテキストで操作を実行する必要があります。 既定では、Visual Basic の算術演算とオーバーフローがチェックされます。C# と F# では、これらはではありません。 オフのコンテキストで操作が発生した場合、変換先の型に収まらない上位ビットを破棄することで結果が切り捨てられます。 次の例は、C# または F# でのこのようなオフの変換を示しています。 前の例をオフのコンテキストで繰り返します。

byte value = 241;
try {
   sbyte newValue = (sbyte) value;
   Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                     value.GetType().Name, value,
                     newValue.GetType().Name, newValue);
}
catch (OverflowException) {
   Console.WriteLine("Exception: {0} > {1}.", value, SByte.MaxValue);
}
// The example displays the following output:
//       Converted the Byte value 241 to the SByte value -15.
let value = 241uy
try
    let newValue = int8 value
    printfn $"Converted the {value.GetType().Name} value {value} to the {newValue.GetType().Name} value {newValue}."
with :? OverflowException ->
    printfn $"Exception: {value} > {SByte.MaxValue}."
// The example displays the following output:
//       Converted the Byte value 241 to the SByte value -15.

次のMicrosoft中間言語 (MSIL) 命令では、 がスローされますOverflowException

  • add.ovf.<署名>

  • conv.ovf.<入力する>

  • conv.ovf.<入力する>.un

  • mul.ovf.<型>

  • sub.ovf.<型>

  • newarr

OverflowException は、値が0x80131516を持つ HRESULT COR_E_OVERFLOWを使用します。

インスタンスの初期プロパティ値の一覧についてはOverflowExceptionを参照してください、OverflowExceptionコンス トラクター。

コンストラクター

OverflowException()

OverflowException クラスの新しいインスタンスを初期化します。

OverflowException(SerializationInfo, StreamingContext)

シリアル化したデータを使用して、OverflowException クラスの新しいインスタンスを初期化します。

OverflowException(String)

指定したエラー メッセージを使用して、OverflowException クラスの新しいインスタンスを初期化します。

OverflowException(String, Exception)

指定したエラー メッセージおよびこの例外の原因となった内部例外への参照を使用して、OverflowException クラスの新しいインスタンスを初期化します。

プロパティ

Data

例外に関する追加のユーザー定義情報を提供する、キーと値のペアのコレクションを取得します。

(継承元 Exception)
HelpLink

この例外に関連付けられているヘルプ ファイルへのリンクを取得または設定します。

(継承元 Exception)
HResult

特定の例外に割り当てられているコード化数値である HRESULT を取得または設定します。

(継承元 Exception)
InnerException

現在の例外の原因となる Exception インスタンスを取得します。

(継承元 Exception)
Message

現在の例外を説明するメッセージを取得します。

(継承元 Exception)
Source

エラーの原因となるアプリケーションまたはオブジェクトの名前を取得または設定します。

(継承元 Exception)
StackTrace

呼び出し履歴で直前のフレームの文字列形式を取得します。

(継承元 Exception)
TargetSite

現在の例外がスローされたメソッドを取得します。

(継承元 Exception)

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetBaseException()

派生クラスでオーバーライドされた場合、それ以後に発生する 1 つ以上の例外の根本原因である Exception を返します。

(継承元 Exception)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetObjectData(SerializationInfo, StreamingContext)

派生クラスでオーバーライドされた場合は、その例外に関する情報を使用して SerializationInfo を設定します。

(継承元 Exception)
GetType()

現在のインスタンスのランタイム型を取得します。

(継承元 Exception)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在の例外の文字列形式を作成して返します。

(継承元 Exception)

イベント

SerializeObjectState
古い.

例外がシリアル化され、例外に関するシリアル化されたデータを含む例外状態オブジェクトが作成されたときに発生します。

(継承元 Exception)

適用対象

こちらもご覧ください