RegexMatchTimeoutException 类   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
正则表达式模式匹配方法的执行时间超出其超时时间间隔时引发的异常。
public ref class RegexMatchTimeoutException : TimeoutExceptionpublic class RegexMatchTimeoutException : TimeoutException[System.Serializable]
public class RegexMatchTimeoutException : TimeoutExceptiontype RegexMatchTimeoutException = class
    inherit TimeoutExceptiontype RegexMatchTimeoutException = class
    inherit TimeoutException
    interface ISerializable[<System.Serializable>]
type RegexMatchTimeoutException = class
    inherit TimeoutException
    interface ISerializablePublic Class RegexMatchTimeoutException
Inherits TimeoutException- 继承
- 继承
- 属性
- 实现
示例
以下示例演示了处理异常的 RegexMatchTimeoutException 两种可能方法。 值为两秒的常量定义最大超时间隔。 最初 Regex.IsMatch(String, String, RegexOptions, TimeSpan) 调用 方法时,超时间隔为 1 秒。 每个异常都 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 属性获取),以便可以调查问题并修改正则表达式模式。 由于过度回溯而导致的超时始终可重现。 
- 如果超时阈值设置过低导致超时,可以增加超时间隔并重试匹配操作。 当前超时间隔可从 MatchTimeout 属性获取。 RegexMatchTimeoutException引发异常时,正则表达式引擎将保持其状态,以便将来的任何调用都返回相同的结果,就像异常未发生一样。 建议的模式是在引发异常后等待短暂的随机时间间隔,然后再再次调用匹配方法。 这可以重复多次。 但是,如果超时是由过度回溯引起的,则重复次数应较小。 
下一节中的示例演示了处理 的这两种 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) | 使用序列化 RegexMatchTimeoutException 对象所需的数据填充 SerializationInfo 对象。 |