谢谢大家的帮助。
当除数为 1 时,此代码不会产生我所期望的结果。未调用 exceptOne 的基类,未显示 exceptOne 中的超链接。我错过了什么?!
控制台输出为:
输入除数
1
WriteLine 异常 1...
WriteLine 异常 2...
基 ctor2
http : // exc2.com
最后写入行
class Program
{
static void Main(string[] args)
{
try
{
byte y = 0;
byte x = 10;
Console.WriteLine("enter a divisor");
string s = (Console.ReadLine());
y = Convert.ToByte(s);
if (y == 1) throw new ExceptOne();
Console.WriteLine("result is {0}", x / y); ;
}
catch (System.DivideByZeroException e)
{
Console.WriteLine("exception occured {0}...", e.Message);
}
catch (ExceptOne p)
{
Console.WriteLine(p.Message +"\n"+ p.HelpLink);
}
catch (System.Exception r)
{
Console.WriteLine(r.Message + "\n" + r.HelpLink);
}
finally
{
Console.WriteLine("Writeline in finally ");
Console.ReadLine();
}
}
}
public class ExceptOne : System.Exception
{
public ExceptOne()
: base("base ctor 1 ")
{
this.HelpLink = "http://exc1.com";
Console.WriteLine("WriteLine exception 1...");
throw new Exception2();
}
}
public class Exception2 : System.Exception
{
public Exception2()
: base("base ctor2 ")
{
Console.WriteLine("WriteLine exception 2...");
this.HelpLink = "http://exc2.com";
}
}