谢谢大家的帮助。c#例外问题
当除数为1时,此代码不会生成我期望的内容.ExceptOne的基类不会被调用,但ExceptOne中的超链接不会显示。我错过了什么?!
控制台输出是:
输入除数
的WriteLine例外1 ...
的WriteLine例外2 ...
基ctor2
HTTP:// exc2.com
Writeline in finally
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";
}
}
谢谢,刚学这东西。然而,Writeline语句在该构造函数中得到执行... – steelponey 2012-01-12 09:49:50
当然,在此之后您抛出异常......在构造函数中抛出异常不会阻止代码被执行(最多抛出),但它将阻止该对象被创建。 – 2012-01-12 09:51:37
是的;尝试评论抛出新的Exception2(); ..你为什么写这个? – 2012-01-12 10:02:45