📌  相关文章
📜  C#SystemException

📅  最后修改于: 2020-10-31 04:08:17             🧑  作者: Mango

C#SystemException类

SystemException是C#中预定义的异常类。它用于处理系统相关的异常。它用作系统异常名称空间的基类。它具有各种子类,例如:ValidationException,ArgumentException,ArithmeticException,DataException,StackOverflowException等。

它由我们在下面列出的丰富的构造函数,属性和方法组成。

C#SystemException签名

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class SystemException : Exception

C#SystemException构造函数

Constructors Description
SystemException() It is used to initialize a new instance of the SystemException class.
SystemException(SerializationInfo,StreamingContext) It is used to initialize a new instance of the SystemException class with serialized data.
SystemException(String) It is used to initialize a new instance of the SystemException class with a specified error message.
SystemException(String,Exception) It is used to initialize a new instance of the SystemException class with a specified error message and a reference to the inner exception that is the cause of this exception.

C#SystemException属性

Property Description
Data It is used to get a collection of key/value pairs that provide additional user-defined information about the exception.
HelpLink It is used to get or set a link to the help file associated with this exception.
HResult It is used to get or set HRESULT, a coded numerical value that is assigned to a specific exception.
InnerException It is used to get the Exception instance that caused the current exception.
Message It is used to get a message that describes the current exception.
Source It is used to get or set the name of the application that causes the error.
StackTrace It is used to get a string representation of the immediate frames on the call stack.
TargetSite It is used to get the method that throws the current exception.

C#SystemException方法

Method Description
Equals(Object) It is used to check that the specified object is equal to the current object or not.
Finalize() It is used to free resources and perform cleanup operations.
GetBaseException() It is used to get root exception.
GetHashCode() It is used to get hash code.
GetObjectData(SerializationInfo,StreamingContext) It is used to get object data.
GetType() It is used to get the runtime type of the current instance.
MemberwiseClone() It is used to create a shallow copy of the current Object.
ToString() It is used to create and return a string representation of the current exception.

C#SystemException示例

此类可用于处理子类的异常。在此,在下面的程序中,程序将引发IndexOutOfRangeException,它是SystemException类的子类。

using System;
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int[] arr = new int[5];
                arr[10] = 25;
            }
            catch (SystemException e)
            {
                Console.WriteLine(e);
            }
        }
    }
}

输出:

System.IndexOutOfRangeException:索引超出数组的范围。