RegexMatchTimeoutException 類別

定義

當規則運算式模式比對方法的執行時間超過其逾時間隔時,所擲回的例外狀況。

public ref class RegexMatchTimeoutException : TimeoutException
public class RegexMatchTimeoutException : TimeoutException
[System.Serializable]
public class RegexMatchTimeoutException : TimeoutException
type RegexMatchTimeoutException = class
    inherit TimeoutException
type RegexMatchTimeoutException = class
    inherit TimeoutException
    interface ISerializable
[<System.Serializable>]
type RegexMatchTimeoutException = class
    inherit TimeoutException
    interface ISerializable
Public Class RegexMatchTimeoutException
Inherits TimeoutException
繼承
RegexMatchTimeoutException
繼承
RegexMatchTimeoutException
屬性
實作

範例

下列範例說明處理例外狀況 RegexMatchTimeoutException 的兩種可能方法。 常數,其值為兩秒,定義最大逾時間隔。 一開始會 Regex.IsMatch(String, String, RegexOptions, TimeSpan) 以一秒的逾時間隔呼叫 方法。 如果目前的逾時間隔小於最大逾時間隔,則每個 RegexMatchTimeoutException 例外狀況都會增加一秒,並導致另一次呼叫 Regex.IsMatch 方法。 不過,如果目前的逾時間隔超過逾時間隔上限,例外狀況處理常式會將資訊寫入事件記錄檔,並放棄正則運算式的處理。

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Security;
using System.Text.RegularExpressions;
using System.Threading;

public class Example
{
   const int MaxTimeoutInSeconds = 2;
   
   public static void Main()
   {
      TimeSpan timeout = new TimeSpan(0, 0, 1);
      string input = "aaaaaaaaaaaaaaaaaaaaaa>";
      if (ValidateInput(input, timeout))
         // Perform some operation with valid input string.
         Console.WriteLine("'{0}' is a valid string.", input); 
   } 

   private static bool ValidateInput(string input, TimeSpan timeout)
   {
      string pattern = "(a+)+$";      
      try {
         return Regex.IsMatch(input, pattern, 
                              RegexOptions.IgnoreCase, timeout);
      }
      catch (RegexMatchTimeoutException e) {
         // Increase the timeout interval and retry.
         timeout = timeout.Add(new TimeSpan(0, 0, 1));
         Console.WriteLine("Changing the timeout interval to {0}", 
                           timeout); 
         if (timeout.TotalSeconds <= MaxTimeoutInSeconds) {
            // Pause for a short period.
            Thread.Sleep(250);
            return ValidateInput(input, timeout);
         }   
         else {
            Console.WriteLine("Timeout interval of {0} exceeded.", 
                              timeout);
            // Write to event log named RegexTimeouts
            try {
               if (! EventLog.SourceExists("RegexTimeouts"))
                  EventLog.CreateEventSource("RegexTimeouts", "RegexTimeouts");

               EventLog log = new EventLog("RegexTimeouts");
               log.Source = "RegexTimeouts";
               string msg = String.Format("Timeout after {0} matching '{1}' with '{2}.",
                                          e.MatchTimeout, e.Input, e.Pattern);
               log.WriteEntry(msg, EventLogEntryType.Error);
            }
            // Do nothing to handle the exceptions.
            catch (SecurityException) { }
            catch (InvalidOperationException) { }
            catch (Win32Exception) { }
            return false;
         }   
      }
   }
}
// The example writes to the event log and also displays the following output:
//       Changing the timeout interval to 00:00:02
//       Changing the timeout interval to 00:00:03
//       Timeout interval of 00:00:03 exceeded.
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Security
Imports System.Text.RegularExpressions
Imports System.Threading

Module Example
   Const MaxTimeoutInSeconds As Integer = 2
   
   Public Sub Main()
      Dim timeout As TimeSpan = New TimeSpan(0, 0, 1)
      
      Dim input As String = "aaaaaaaaaaaaaaaaaaaaaa>"
      If ValidateInput(input, timeout) Then
         ' Perform some operation with valid input string.
         Console.WriteLine("'{0}' is a valid string.", input) 
      End If
   End Sub 

   Private Function ValidateInput(input As String, 
                                  timeout As TimeSpan) As Boolean
      Dim pattern As String = "(a+)+$"      
      Try
         Return Regex.IsMatch(input, pattern, 
                              RegexOptions.IgnoreCase, timeout)
      Catch e As RegexMatchTimeoutException
         ' Increase the timeout interval and retry.
         timeout = timeout.Add(New TimeSpan(0, 0, 1))
         Console.WriteLine("Changing the timeout interval to {0}", 
                           timeout) 
         If timeout.TotalSeconds <= MaxTimeoutInSeconds Then
            ' Pause for a short interval.
            Thread.Sleep(250)
            Return ValidateInput(input, timeout)
         Else
            Console.WriteLine("Timeout interval of {0} exceeded.", 
                              timeout)
            ' Write to event log named RegexTimeouts
            Try
               If Not EventLog.SourceExists("RegexTimeouts") Then
                  EventLog.CreateEventSource("RegexTimeouts", "RegexTimeouts")
               End If   
               Dim log As New EventLog("RegexTimeouts")
               log.Source = "RegexTimeouts"
               Dim msg As String = String.Format("Timeout after {0} matching '{1}' with '{2}.",
                                                 e.MatchTimeout, e.Input, e.Pattern)
               log.WriteEntry(msg, EventLogEntryType.Error)
            ' Do nothing to handle the exceptions.
            Catch ex As SecurityException

            Catch ex As InvalidOperationException

            Catch ex As Win32Exception

            End Try   
            Return False
         End If   
      End Try
   End Function
End Module
' The example writes to the event log and also displays the following output:
'       Changing the timeout interval to 00:00:02
'       Changing the timeout interval to 00:00:03
'       Timeout interval of 00:00:03 exceeded.

備註

例外狀況 RegexMatchTimeoutException 的存在通常表示下列其中一個條件:

  • 正則運算式引擎會過度回溯,因為它嘗試比對輸入文字與正則運算式模式。

  • 逾時間隔已設定太低,特別是因為機器負載偏高。

例外狀況處理常式處理例外狀況的方式取決於例外狀況的原因:

  • 如果逾時結果來自過多回溯,您的例外狀況處理常式應該放棄嘗試比對輸入,並通知使用者正則運算式模式比對方法中已發生逾時。 可能的話,應該記錄來自 屬性的正則運算式模式 Pattern 相關資訊,以及造成過度回溯的輸入,而該模式可從 Input 屬性取得,以便調查問題並修改正則運算式模式。 由於過度回溯而造成的逾時一律可重現。

  • 如果逾時導致設定逾時臨界值太低,您可以增加逾時間隔,然後重試比對作業。 目前逾時間隔可從 屬性取得 MatchTimeoutRegexMatchTimeoutException擲回例外狀況時,正則運算式引擎會維護其狀態,讓任何未來的調用傳回相同的結果,就如同未發生例外狀況一樣。 建議的模式是在擲回例外狀況之後等候短暫的隨機時間間隔,然後再再次呼叫比對方法。 這可以重複數次。 不過,如果逾時是因為過度回溯所造成,重複次數應該很小。

下一節中的範例說明這兩種 RegexMatchTimeoutException 處理 的技術。

建構函式

RegexMatchTimeoutException()

使用系統提供的錯誤,初始化 RegexMatchTimeoutException 類別的新執行個體。

RegexMatchTimeoutException(SerializationInfo, StreamingContext)

使用序列化資料,初始化 RegexMatchTimeoutException 類別的新執行個體。

RegexMatchTimeoutException(String)

使用指定的訊息字串,初始化 RegexMatchTimeoutException 類別的新執行個體。

RegexMatchTimeoutException(String, Exception)

使用指定的錯誤訊息以及造成此例外狀況的內部例外狀況的參考,初始化 RegexMatchTimeoutException 類別的新執行個體。

RegexMatchTimeoutException(String, String, TimeSpan)

使用規則運算式模式、輸入文字,以及逾時間隔的相關資訊,初始化 RegexMatchTimeoutException 類別的新執行個體。

屬性

Data

取得鍵值組的集合,這些鍵值組會提供關於例外狀況的其他使用者定義資訊。

(繼承來源 Exception)
HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。

(繼承來源 Exception)
HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。

(繼承來源 Exception)
InnerException

取得造成目前例外狀況的 Exception 執行個體。

(繼承來源 Exception)
Input

取得當發生逾時,規則運算式引擎所處理的輸入文字。

MatchTimeout

取得規則運算式比對的逾時間隔。

Message

取得描述目前例外狀況的訊息。

(繼承來源 Exception)
Pattern

取得當發生逾時,比對作業中所使用的規則運算式模式。

Source

取得或設定造成錯誤的應用程式或物件的名稱。

(繼承來源 Exception)
StackTrace

取得呼叫堆疊上即時運算框架的字串表示。

(繼承來源 Exception)
TargetSite

取得擲回目前例外狀況的方法。

(繼承來源 Exception)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 Exception

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

在衍生類別中覆寫時,使用例外狀況的資訊設定 SerializationInfo

(繼承來源 Exception)
GetType()

取得目前執行個體的執行階段類型。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

建立並傳回目前例外狀況的字串表示。

(繼承來源 Exception)

事件

SerializeObjectState
已淘汰.

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。

(繼承來源 Exception)

明確介面實作

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

將序列化 SerializationInfo 物所需的資料填入 RegexMatchTimeoutException 物件。

適用於

另請參閱