Bagikan melalui


OverflowException Kelas

Definisi

Pengecualian yang dilemparkan ketika operasi aritmatika, transmisi, atau konversi dalam konteks yang diperiksa menghasilkan luapan.

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
Warisan
Warisan
Atribut

Keterangan

OverflowException dilemparkan pada waktu proses dalam kondisi berikut:

  • Operasi aritmatika menghasilkan hasil yang berada di luar rentang jenis data yang dikembalikan oleh operasi. Contoh berikut mengilustrasikan OverflowException yang dilemparkan oleh operasi perkalian yang meluap ke batas jenis 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.
    
  • Operasi transmisi atau konversi mencoba melakukan konversi yang mempersempit, dan nilai jenis data sumber berada di luar rentang jenis data target. Contoh berikut mengilustrasikan OverflowException yang dilemparkan oleh upaya untuk mengonversi nilai byte besar yang tidak ditandatangani menjadi nilai byte yang ditandatangani.

    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.
    

Dalam setiap kasus, hasil operasi adalah nilai yang kurang dari properti MinValue atau lebih besar dari properti MaxValue dari jenis data yang dihasilkan dari operasi.

Agar operasi aritmatika, transmisi, atau konversi melemparkan OverflowException, operasi harus terjadi dalam konteks yang diperiksa. Secara default, operasi aritmatika dan luapan di Visual Basic diperiksa; di C# dan F#, mereka tidak. Jika operasi terjadi dalam konteks yang tidak dicentang, hasilnya dipotong dengan membuang bit berurutan tinggi yang tidak sesuai dengan jenis tujuan. Contoh berikut mengilustrasikan konversi yang tidak dicentang seperti itu di C# atau F#. Ini mengulangi contoh sebelumnya dalam konteks yang tidak dicentang.

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.

Instruksi bahasa perantara Microsoft (MSIL) berikut melemparkan OverflowException:

  • yang ditandatangani

  • conv.ovf. <untuk mengetik>

  • conv.ovf. <untuk mengetik>.un

  • jenis

  • jenis

  • newarr

OverflowException menggunakan COR_E_OVERFLOW HRESULT, yang memiliki nilai 0x80131516.

Untuk daftar nilai properti awal untuk instans OverflowException, lihat konstruktor OverflowException.

Konstruktor

OverflowException()

Menginisialisasi instans baru kelas OverflowException.

OverflowException(SerializationInfo, StreamingContext)
Kedaluwarsa.

Menginisialisasi instans baru kelas OverflowException dengan data berseri.

OverflowException(String)

Menginisialisasi instans baru kelas OverflowException dengan pesan kesalahan tertentu.

OverflowException(String, Exception)

Menginisialisasi instans baru kelas OverflowException dengan pesan kesalahan tertentu dan referensi ke pengecualian dalam yang merupakan penyebab pengecualian ini.

Properti

Data

Mendapatkan kumpulan pasangan kunci/nilai yang memberikan informasi tambahan yang ditentukan pengguna tentang pengecualian.

(Diperoleh dari Exception)
HelpLink

Mendapatkan atau mengatur tautan ke file bantuan yang terkait dengan pengecualian ini.

(Diperoleh dari Exception)
HResult

Mendapatkan atau mengatur HRESULT, nilai numerik berkode yang ditetapkan ke pengecualian tertentu.

(Diperoleh dari Exception)
InnerException

Mendapatkan instans Exception yang menyebabkan pengecualian saat ini.

(Diperoleh dari Exception)
Message

Mendapatkan pesan yang menjelaskan pengecualian saat ini.

(Diperoleh dari Exception)
Source

Mendapatkan atau mengatur nama aplikasi atau objek yang menyebabkan kesalahan.

(Diperoleh dari Exception)
StackTrace

Mendapatkan representasi string dari bingkai langsung pada tumpukan panggilan.

(Diperoleh dari Exception)
TargetSite

Mendapatkan metode yang melemparkan pengecualian saat ini.

(Diperoleh dari Exception)

Metode

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GetBaseException()

Saat ditimpa dalam kelas turunan, mengembalikan Exception yang merupakan akar penyebab dari satu atau beberapa pengecualian berikutnya.

(Diperoleh dari Exception)
GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetObjectData(SerializationInfo, StreamingContext)
Kedaluwarsa.

Saat ditimpa di kelas turunan, mengatur SerializationInfo dengan informasi tentang pengecualian.

(Diperoleh dari Exception)
GetType()

Mendapatkan jenis runtime instans saat ini.

(Diperoleh dari Exception)
MemberwiseClone()

Membuat salinan dangkal dari Objectsaat ini.

(Diperoleh dari Object)
ToString()

Membuat dan mengembalikan representasi string dari pengecualian saat ini.

(Diperoleh dari Exception)

Acara

SerializeObjectState
Kedaluwarsa.

Terjadi ketika pengecualian diserialisasikan untuk membuat objek status pengecualian yang berisi data berseri tentang pengecualian.

(Diperoleh dari Exception)

Berlaku untuk

Lihat juga