StackOverflowException Kelas

Definisi

Pengecualian yang dilemparkan ketika tumpukan eksekusi melebihi ukuran tumpukan. Kelas ini tidak dapat diwariskan.

public ref class StackOverflowException sealed : SystemException
public sealed class StackOverflowException : SystemException
[System.Serializable]
public sealed class StackOverflowException : SystemException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class StackOverflowException : SystemException
type StackOverflowException = class
    inherit SystemException
[<System.Serializable>]
type StackOverflowException = class
    inherit SystemException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StackOverflowException = class
    inherit SystemException
Public NotInheritable Class StackOverflowException
Inherits SystemException
Warisan
StackOverflowException
Atribut

Contoh

Contoh berikut menggunakan penghitung untuk memastikan bahwa jumlah panggilan rekursif ke Execute metode tidak melebihi maksimum yang ditentukan oleh konstanta MAX_RECURSIVE_CALLS.

using System;

public class Example
{
   private const int MAX_RECURSIVE_CALLS = 1000;
   static int ctr = 0;
   
   public static void Main()
   {
      Example ex = new Example();
      ex.Execute();
      Console.WriteLine("\nThe call counter: {0}", ctr);
   }

   private void Execute()
   {
      ctr++;
      if (ctr % 50 == 0)
         Console.WriteLine("Call number {0} to the Execute method", ctr);
         
      if (ctr <= MAX_RECURSIVE_CALLS)
         Execute();
         
      ctr--;
   }
}
// The example displays the following output:
//       Call number 50 to the Execute method
//       Call number 100 to the Execute method
//       Call number 150 to the Execute method
//       Call number 200 to the Execute method
//       Call number 250 to the Execute method
//       Call number 300 to the Execute method
//       Call number 350 to the Execute method
//       Call number 400 to the Execute method
//       Call number 450 to the Execute method
//       Call number 500 to the Execute method
//       Call number 550 to the Execute method
//       Call number 600 to the Execute method
//       Call number 650 to the Execute method
//       Call number 700 to the Execute method
//       Call number 750 to the Execute method
//       Call number 800 to the Execute method
//       Call number 850 to the Execute method
//       Call number 900 to the Execute method
//       Call number 950 to the Execute method
//       Call number 1000 to the Execute method
//
//       The call counter: 0
let MAX_RECURSIVE_CALLS = 1000
let mutable ctr = 0
   
let rec execute () =
    ctr <- ctr + 1
    if ctr % 50 = 0 then
        printfn $"Call number {ctr} to the Execute method"
        
    if ctr <= MAX_RECURSIVE_CALLS then
        execute ()
        
    ctr <- ctr - 1
    
execute ()
printfn $"\nThe call counter: {ctr}"
// The example displays the following output:
//       Call number 50 to the Execute method
//       Call number 100 to the Execute method
//       Call number 150 to the Execute method
//       Call number 200 to the Execute method
//       Call number 250 to the Execute method
//       Call number 300 to the Execute method
//       Call number 350 to the Execute method
//       Call number 400 to the Execute method
//       Call number 450 to the Execute method
//       Call number 500 to the Execute method
//       Call number 550 to the Execute method
//       Call number 600 to the Execute method
//       Call number 650 to the Execute method
//       Call number 700 to the Execute method
//       Call number 750 to the Execute method
//       Call number 800 to the Execute method
//       Call number 850 to the Execute method
//       Call number 900 to the Execute method
//       Call number 950 to the Execute method
//       Call number 1000 to the Execute method
//
//       The call counter: 0
Module Example
   Private Const MAX_RECURSIVE_CALLS As Integer = 1000
   Dim ctr As Integer = 0

   Public Sub Main()
      Execute()
      Console.WriteLine()
      Console.WriteLine("The call counter: {0}", ctr)
   End Sub

   Private Sub Execute()
      ctr += 1
      If ctr Mod 50 = 0 Then
         Console.WriteLine("Call number {0} to the Execute method", ctr)
      End If
      
      If ctr <= MAX_RECURSIVE_CALLS Then
         Execute()
      End If

      ctr -= 1
   End Sub
End Module
' The example displays the following output:
'       Call number 50 to the Execute method
'       Call number 100 to the Execute method
'       Call number 150 to the Execute method
'       Call number 200 to the Execute method
'       Call number 250 to the Execute method
'       Call number 300 to the Execute method
'       Call number 350 to the Execute method
'       Call number 400 to the Execute method
'       Call number 450 to the Execute method
'       Call number 500 to the Execute method
'       Call number 550 to the Execute method
'       Call number 600 to the Execute method
'       Call number 650 to the Execute method
'       Call number 700 to the Execute method
'       Call number 750 to the Execute method
'       Call number 800 to the Execute method
'       Call number 850 to the Execute method
'       Call number 900 to the Execute method
'       Call number 950 to the Execute method
'       Call number 1000 to the Execute method
'
'       The call counter: 0

Keterangan

StackOverflowException dilemparkan untuk kesalahan luapan tumpukan eksekusi, biasanya jika terjadi rekursi yang sangat dalam atau tidak terbatas. Jadi pastikan kode Anda tidak memiliki perulangan tak terbatas atau rekursi tak terbatas.

StackOverflowException menggunakan COR_E_STACKOVERFLOW HRESULT, yang memiliki nilai 0x800703E9. Instruksi Localloc bahasa perantara (IL) melempar StackOverflowException. Untuk daftar nilai properti awal untuk StackOverflowException objek, lihat StackOverflowException konstruktor.

Dimulai dengan .NET Framework 2.0, Anda tidak dapat menangkap StackOverflowException objek dengan try/catch blok, dan proses yang sesuai dihentikan secara default. Akibatnya, Anda harus menulis kode Anda untuk mendeteksi dan mencegah luapan tumpukan. Misalnya, jika aplikasi Anda bergantung pada rekursi, gunakan penghitung atau kondisi status untuk mengakhiri perulangan rekursif. Lihat bagian Contoh untuk ilustrasi teknik ini.

Catatan

Menerapkan atribut ke HandleProcessCorruptedStateExceptionsAttribute metode yang melempar tidak berpengaruh StackOverflowException . Anda masih tidak dapat menangani pengecualian dari kode pengguna.

Jika aplikasi Anda menghosting runtime bahasa umum (CLR), aplikasi dapat menentukan bahwa CLR harus membongkar domain aplikasi tempat pengecualian luapan tumpukan terjadi dan membiarkan proses yang sesuai berlanjut. Untuk informasi selengkapnya, lihat Antarmuka ICLRPolicyManager.

Konstruktor

StackOverflowException()

Menginisialisasi instans StackOverflowException baru kelas, mengatur Message properti instans baru ke pesan yang disediakan sistem yang menjelaskan kesalahan, seperti "Operasi yang diminta menyebabkan luapan tumpukan." Pesan ini memperhitungkan budaya sistem saat ini.

StackOverflowException(String)

Menginisialisasi instans StackOverflowException baru kelas dengan pesan kesalahan tertentu.

StackOverflowException(String, Exception)

Menginisialisasi instans StackOverflowException baru kelas 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

Get dan set 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()

Ketika ditimpa di kelas turunan Exception , mengembalikan 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 SerializationInfo , mengatur dengan informasi tentang pengecualian.

(Diperoleh dari Exception)
GetType()

Mendapatkan jenis runtime dari instans saat ini.

(Diperoleh dari Exception)
MemberwiseClone()

Membuat salinan dangkal dari saat ini Object.

(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