📄 ex-11-08
字号:
// Example 11-08: Rethrowing and inner exceptions
namespace Programming_CSharp
{
using System;
public class MyCustomException : System.Exception
{
public MyCustomException(
string message,Exception inner):
base(message,inner)
{
}
}
public class Test
{
public static void Main()
{
Test t = new Test();
t.TestFunc();
}
public void TestFunc()
{
try
{
DangerousFunc1();
}
// if you catch a custom exception
// print the exception history
catch (MyCustomException e)
{
Console.WriteLine("\n{0}", e.Message);
Console.WriteLine(
"Retrieving exception history...");
Exception inner =
e.InnerException;
while (inner != null)
{
Console.WriteLine(
"{0}",inner.Message);
inner =
inner.InnerException;
}
}
}
public void DangerousFunc1()
{
try
{
DangerousFunc2();
}
// if you catch any exception here
// throw a custom exception
catch(System.Exception e)
{
MyCustomException ex =
new MyCustomException(
"E3 - Custom Exception Situation!",e);
throw ex;
}
}
public void DangerousFunc2()
{
try
{
DangerousFunc3();
}
// if you catch a DivideByZeroException take some
// corrective action and then throw a general exception
catch (System.DivideByZeroException e)
{
Exception ex =
new Exception(
"E2 - Func2 caught divide by zero",e);
throw ex;
}
}
public void DangerousFunc3()
{
try
{
DangerousFunc4();
}
catch (System.ArithmeticException)
{
throw;
}
catch (System.Exception)
{
Console.WriteLine(
"Exception handled here.");
}
}
public void DangerousFunc4()
{
throw new DivideByZeroException("E1 - DivideByZero Exception");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -