clr via c# 异常和状态管理
2021-01-11 05:32
标签:console over read 状态 flag padding obj zh-cn 测试 1,System.Exception 类---所有异常类的基类 Message 如果当前异常时在处理一个异常时抛出的,则指出上个异常是什么 可以使用Exception.GetBaseException来遍历异常链表.
1.1 Exception.GetBaseException 方法用法 2,自定义异常类及其使用方法: 3,建立自己的ExceptionArgs的派生类,用于携带异常信息. 4,使用方法: 5,结果 clr via c# 异常和状态管理 标签:console over read 状态 flag padding obj zh-cn 测试 原文地址:https://www.cnblogs.com/frogkiller/p/12291485.html
readonly
string
指出异常的原因
Data
readonly
IDictionary
引用一个键值对集合
Source
r/w
string
包含异常的程序集名称
StackTrace
r
string
包含异常之前调用所有方法和信息
TargetSite
r
MethodBase
包含抛出异常的方法
InnerException
r
Exception
HResult
r/w
int
COM api 返回值维护.
class SecondLevelException : Exception
{
public SecondLevelException(string message, Exception inner)//使用Message和InnerExciption来初始化异常
: base(message, inner)
{ }
}
class ThirdLevelException : Exception//使用Message和InnerExciption来初始化异常
{
public ThirdLevelException(string message, Exception inner)
: base(message, inner)
{ }
}
static void DivideBy0()
{
try
{
int zero = 0;
int ecks = 1 / zero;
}
catch (Exception ex)
{
throw new SecondLevelException(
"Forced a division by 0 and threw " +
"a second exception.", ex);
}
}
static void Rethrow()
{
try
{
DivideBy0();
}
catch (Exception ex)
{
throw new ThirdLevelException(
"Caught the second exception and " +
"threw a third in response.", ex);
}
}
public static void Go()
{
try
{
// This function calls another that forces a
// division by 0.
Rethrow();
}
catch (Exception ex)
{
Exception current;
// This code unwinds the nested exceptions using the
// InnerException property.
current = ex;
while (current != null)
{
Console.WriteLine(current.ToString());
Console.WriteLine();
current = current.InnerException;//调用当前异常的上个异常.
}
// Display the innermost exception.
Console.WriteLine(
"Display the base exception " +
"using the GetBaseException method:\n");
Console.WriteLine(
ex.GetBaseException().ToString());
}
}
ClrFromCSharp_2_2.LearnException.ThirdLevelException: Caught the second exception and threw a third in response. ---> ClrFromCSharp_2_2.LearnException.SecondLevelException: Forced a division by 0 and threw a second exception. ---> System.DivideByZeroException: 尝试除以零。
在 ClrFromCSharp_2_2.LearnException.ExceptionRef.DivideBy0() 位置 C:\reps\ClrFromCSharp\ClrFromCSharp_2_2\LearnException\ExceptionRef.cs:行号 62
--- 内部异常堆栈跟踪的结尾 ---
在 ClrFromCSharp_2_2.LearnException.ExceptionRef.DivideBy0() 位置 C:\reps\ClrFromCSharp\ClrFromCSharp_2_2\LearnException\ExceptionRef.cs:行号 66
在 ClrFromCSharp_2_2.LearnException.ExceptionRef.Rethrow() 位置 C:\reps\ClrFromCSharp\ClrFromCSharp_2_2\LearnException\ExceptionRef.cs:行号 48
--- 内部异常堆栈跟踪的结尾 ---
在 ClrFromCSharp_2_2.LearnException.ExceptionRef.Rethrow() 位置 C:\reps\ClrFromCSharp\ClrFromCSharp_2_2\LearnException\ExceptionRef.cs:行号 52
在 ClrFromCSharp_2_2.LearnException.ExceptionRef.Go() 位置 C:\reps\ClrFromCSharp\ClrFromCSharp_2_2\LearnException\ExceptionRef.cs:行号 19---从最初异常---至抛出异常的一个trace.
ClrFromCSharp_2_2.LearnException.SecondLevelException: Forced a division by 0 and threw a second exception. ---> System.DivideByZeroException: 尝试除以零。
在 ClrFromCSharp_2_2.LearnException.ExceptionRef.DivideBy0() 位置 C:\reps\ClrFromCSharp\ClrFromCSharp_2_2\LearnException\ExceptionRef.cs:行号 62
--- 内部异常堆栈跟踪的结尾 ---
在 ClrFromCSharp_2_2.LearnException.ExceptionRef.DivideBy0() 位置 C:\reps\ClrFromCSharp\ClrFromCSharp_2_2\LearnException\ExceptionRef.cs:行号 66
在 ClrFromCSharp_2_2.LearnException.ExceptionRef.Rethrow() 位置 C:\reps\ClrFromCSharp\ClrFromCSharp_2_2\LearnException\ExceptionRef.cs:行号 48//从最初异常到抛出异常的一个base.
System.DivideByZeroException: 尝试除以零。
在 ClrFromCSharp_2_2.LearnException.ExceptionRef.DivideBy0() 位置 C:\reps\ClrFromCSharp\ClrFromCSharp_2_2\LearnException\ExceptionRef.cs:行号 62//从最初异常到抛出异常的一个base.
Display the base exception using the GetBaseException method:
System.DivideByZeroException: 尝试除以零。
在 ClrFromCSharp_2_2.LearnException.ExceptionRef.DivideBy0() 位置 C:\reps\ClrFromCSharp\ClrFromCSharp_2_2\LearnException\ExceptionRef.cs:行号 62//最底层的一个ex
static void Rethrow()
{
try
{
DivideBy0();
}
catch (Exception ex)
{
throw new ThirdLevelException(
"Caught the second exception and " +
"threw a third in response.", ex);
}
}
ex.GetBaseException().ToString());//获取Exception链表的最先抛出的异常.越后面抛出的越前面.
[Serializable]
public sealed class Exception
[Serializable]
public abstract class ExceptionArgs
{
public virtual String Message { get { return string.Empty; } }
}
public sealed class DiskFullExceptionArgs : ExceptionArgs
{
public string Path { get; }
public DiskFullExceptionArgs(string path)
{
Path = path;
}
public override string Message => Path ?? base.Message;
}
public static void GoCustomException()
{
try
{
throw new Exception
the Exception Message is disk is full (c:\)
the ExceptionArgs Message is c:
文章标题:clr via c# 异常和状态管理
文章链接:http://soscw.com/index.php/essay/41348.html