InvalidTimeZoneException 构造函数

定义

初始化 InvalidTimeZoneException 类的新实例。Initializes a new instance of the InvalidTimeZoneException class.

重载

InvalidTimeZoneException()

使用由系统提供的消息初始化 InvalidTimeZoneException 类的新实例。Initializes a new instance of the InvalidTimeZoneException class with a system-supplied message.

InvalidTimeZoneException(String)

使用指定的消息字符串初始化 InvalidTimeZoneException 类的新实例。Initializes a new instance of the InvalidTimeZoneException class with the specified message string.

InvalidTimeZoneException(SerializationInfo, StreamingContext)

用序列化数据初始化 InvalidTimeZoneException 类的新实例。Initializes a new instance of the InvalidTimeZoneException class from serialized data.

InvalidTimeZoneException(String, Exception)

使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 InvalidTimeZoneException 类的新实例。Initializes a new instance of the InvalidTimeZoneException class with a specified error message and a reference to the inner exception that is the cause of this exception.

InvalidTimeZoneException()

使用由系统提供的消息初始化 InvalidTimeZoneException 类的新实例。Initializes a new instance of the InvalidTimeZoneException class with a system-supplied message.

public:
 InvalidTimeZoneException();
public InvalidTimeZoneException ();
Public Sub New ()

注解

这是类的无参数构造函数 InvalidTimeZoneExceptionThis is the parameterless constructor of the InvalidTimeZoneException class. 它将 Message 新实例的属性初始化为系统提供的描述错误的消息,例如,引发了 "InvalidTimeZoneException" 类型的异常。It initializes the Message property of the new instance to a system-supplied message that describes the error, such as "Exception of type 'System.InvalidTimeZoneException' was thrown." 此消息针对当前系统区域性进行了本地化。This message is localized for the current system culture.

适用于

InvalidTimeZoneException(String)

使用指定的消息字符串初始化 InvalidTimeZoneException 类的新实例。Initializes a new instance of the InvalidTimeZoneException class with the specified message string.

public:
 InvalidTimeZoneException(System::String ^ message);
public InvalidTimeZoneException (string message);
public InvalidTimeZoneException (string? message);
new InvalidTimeZoneException : string -> InvalidTimeZoneException
Public Sub New (message As String)

参数

message
String

描述异常的字符串。A string that describes the exception.

注解

作为参数提供的字符串 message 被分配给 Message 属性。The string supplied as the message parameter is assigned to the Message property. 应为当前区域性对其进行本地化。It should be localized for the current culture.

适用于

InvalidTimeZoneException(SerializationInfo, StreamingContext)

用序列化数据初始化 InvalidTimeZoneException 类的新实例。Initializes a new instance of the InvalidTimeZoneException class from serialized data.

protected:
 InvalidTimeZoneException(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
protected InvalidTimeZoneException (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
new InvalidTimeZoneException : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> InvalidTimeZoneException
Protected Sub New (info As SerializationInfo, context As StreamingContext)

参数

info
SerializationInfo

包含序列化数据的对象。The object that contains the serialized data.

context
StreamingContext

包含序列化数据的流。The stream that contains the serialized data.

例外

info 参数为 nullThe info parameter is null.

- 或 --or- context 参数为 nullThe context parameter is null.

注解

此构造函数不由您的代码直接调用来实例化 InvalidTimeZoneException 对象。This constructor is not called directly by your code to instantiate the InvalidTimeZoneException object. 相反,它 IFormatter DeserializeInvalidTimeZoneException 从流中反序列化对象时由对象的方法调用。Instead, it is called by the IFormatter object's Deserialize method when deserializing the InvalidTimeZoneException object from a stream.

适用于

InvalidTimeZoneException(String, Exception)

使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 InvalidTimeZoneException 类的新实例。Initializes a new instance of the InvalidTimeZoneException class with a specified error message and a reference to the inner exception that is the cause of this exception.

public:
 InvalidTimeZoneException(System::String ^ message, Exception ^ innerException);
public InvalidTimeZoneException (string message, Exception innerException);
public InvalidTimeZoneException (string? message, Exception? innerException);
new InvalidTimeZoneException : string * Exception -> InvalidTimeZoneException
Public Sub New (message As String, innerException As Exception)

参数

message
String

描述异常的字符串。A string that describes the exception.

innerException
Exception

导致当前异常的异常。The exception that is the cause of the current exception.

示例

下面的代码尝试检索 TimeZoneInfo 表示中部标准时区的对象。The following code tries to retrieve a TimeZoneInfo object that represents the Central Standard Time zone. 如果 InvalidTimeZoneException 方法调用中发生了 RetrieveTimeZone ,则异常处理程序将异常包装在新的 InvalidTimeZoneException 对象中,并将其返回给调用方。If an InvalidTimeZoneException occurs in the RetrieveTimeZone method call, the exception handler wraps the exception in a new InvalidTimeZoneException object, which it returns to the caller. 然后,调用方的异常处理程序显示有关外部和内部异常的信息。The caller's exception handler then displays information about both the outer and inner exceptions.

private void HandleInnerException()
{   
   string timeZoneName = "Any Standard Time";
   TimeZoneInfo tz;
   try
   {
      tz = RetrieveTimeZone(timeZoneName);
      Console.WriteLine("The time zone display name is {0}.", tz.DisplayName);
   }
   catch (TimeZoneNotFoundException e)
   {
      Console.WriteLine("{0} thrown by application", e.GetType().Name);
      Console.WriteLine("   Message: {0}", e.Message);
      if (e.InnerException != null)
      {
         Console.WriteLine("   Inner Exception Information:");
         Exception innerEx = e.InnerException;
         while (innerEx != null)
         {
            Console.WriteLine("      {0}: {1}", innerEx.GetType().Name, innerEx.Message);
            innerEx = innerEx.InnerException;
         }
      }            
   }   
}

private TimeZoneInfo RetrieveTimeZone(string tzName)
{
   try
   {
      return TimeZoneInfo.FindSystemTimeZoneById(tzName);
   }   
   catch (TimeZoneNotFoundException ex1)
   {
      throw new TimeZoneNotFoundException( 
            String.Format("The time zone '{0}' cannot be found.", tzName), 
            ex1);
   }          
   catch (InvalidTimeZoneException ex2)
   {
      throw new InvalidTimeZoneException( 
            String.Format("The time zone {0} contains invalid data.", tzName), 
            ex2); 
   }      
}
Private Sub HandleInnerException()
   Dim timeZoneName As String = "Any Standard Time"
   Dim tz As TimeZoneInfo
   Try
      tz = RetrieveTimeZone(timeZoneName)
      Console.WriteLine("The time zone display name is {0}.", tz.DisplayName)
   Catch e As TimeZoneNotFoundException
      Console.WriteLine("{0} thrown by application", e.GetType().Name)
      Console.WriteLine("   Message: {0}", e.Message)
      If e.InnerException IsNot Nothing Then
         Console.WriteLine("   Inner Exception Information:")
         Dim innerEx As Exception = e.InnerException
         Do
            Console.WriteLine("      {0}: {1}", innerEx.GetType().Name, innerEx.Message)
            innerEx = innerEx.InnerException
         Loop While innerEx IsNot Nothing
      End If            
   End Try   
End Sub

Private Function RetrieveTimeZone(tzName As String) As TimeZoneInfo
   Try
      Return TimeZoneInfo.FindSystemTimeZoneById(tzName)
   Catch ex1 As TimeZoneNotFoundException
      Throw New TimeZoneNotFoundException( _
            String.Format("The time zone '{0}' cannot be found.", tzName), _
            ex1) 
   Catch ex2 As InvalidTimeZoneException
      Throw New InvalidTimeZoneException( _
            String.Format("The time zone {0} contains invalid data.", tzName), _
            ex2) 
   End Try      
End Function

注解

通常,使用类的此重载 InvalidTimeZoneException 来处理中的异常 trycatchTypically, you use this overload of the InvalidTimeZoneException class to handle an exception in a trycatch 模块.block. innerException参数应是对在块中处理的异常对象的引用 catch ,也可以是 nullThe innerException parameter should be a reference to the exception object handled in the catch block, or it can be null. 然后,将此值分配给 InvalidTimeZoneException 对象的 InnerException 属性。This value is then assigned to the InvalidTimeZoneException object's InnerException property.

message 字符串分配给 Message 属性。The message string is assigned to the Message property. 应为当前区域性对字符串进行本地化。The string should be localized for the current culture.

适用于